Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7191933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:50:00+00:00 2026-05-28T19:50:00+00:00

I have 2 instances x and y of a same class RBnode . Is

  • 0

I have 2 instances x and y of a same class RBnode.
Is there a way to exchange their identities so that all reference to x goes to y and vice versa?

For example,

x = RBnode()
y = RBnode()
x.data = 1
y.data = 2
L = [x,y]

exchange_identity(x,y)
print x.data, y.data, (L[0] is y)

>>> 2 1 True

Actually I’m building an extension of Red-Black tree. Nodes of the tree are implemented as object. When writing the node removal method, I need to exchange two nodes, so that the node to be removed gets “at the bottom” of the tree.

First I tried to just exchange data in the two nodes:

def remove_node(self, y):
    ''' remove node y from tree
    return (y.key,y.value) if successful'''
... ... ...

    # exchange y's data with that of its successor y.next 

    y.key = y.next.key
    y.value = y.next.value

But later an error occurred when remove_node was called and the caller function was holding a reference to a node x, which happens to be y.next. Something like

x = y.next
self.remove_node(y)
x.parent
>>> AttributeError: 'NoneType' object has no attribute 'parent'

I could exchange all corresponding attributes of x and y.
But that takes pretty many lines since the structure of a node is rather complicated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:50:01+00:00Added an answer on May 28, 2026 at 7:50 pm

    I must be missing something, because what’s wrong with this?

    >>> class RBnode(object):
    ...   pass
    ... 
    >>> x = RBnode()
    >>> y = RBnode()
    >>> id(x), id(y)
    (20552592, 20552528)
    >>> x,y = y,x
    >>> id(x), id(y)
    (20552528, 20552592)
    

    Or have you meant for reassigning all the names, like some kind of identity theft of an object? While it would be possible working at the C level, I would be pretty surprised if you could do it from within python without breaking things majorly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three objects that are all the same class. One was created via
I have 3 Oracle database instances with the same data and scheme that I
I have two objects (instances of the same class) with a bunch of properties,
When you have different instances of the same class such as ' FootballTeam '
I have an ArrayList of objects. The objects are all of the same class
I have two QuerySets both containing the instances of the same model class. class
Suppose I have two instances of the same class. The class has a pointer
I have a class named dot and many instances of this same class are
I have many variable-sized lists containing instances of the same class with attribute foo,
I have similar instances of the same web application running across many domains. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.