Python change object reference from object itself

Stefan

Consider the following code:

class A:
    def hi(self):
        print 'A'
    def change(self):
        self = B()
class B(A):
    def hi(self):
        print 'B'

and

test = A()
test.hi() -> prints A
test.change()
test.hi() -> prints A, should print B

Is there some way to make this principle work, so changing the object reference 'test' from withing the class/object itself?

Amber

Objects have no concept of the variable that contains them - thus, you can't do exactly what you're trying to do.

What you could do is have a container that is aware of the thing it contains:

class Container(object):
    def __init__(self):
        self.thing = A()
    def change(self):
        self.thing = B()
    def hi(self):
        self.thing.hi()

test = Container()
test.hi() # prints A
test.change()
test.hi() # prints B

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

c# Is it possible get a reference of an object, get the object itself and change it, instead of assinging to a new object?

From Dev

Java changes reference but not the object itself?

From Dev

Store the value of objects instead of a reference to the object itself

From Dev

Passing object reference as itself to own method explicitly

From Dev

Can a JavaScript object's child reference itself?

From Dev

Reference an object from within the object

From Dev

Python Object Reference

From Dev

Python multiprocessing object reference

From Dev

Python Object Reference

From Java

how hashmap object reference change

From Dev

how hashmap object reference change

From Dev

Reference Object and change it upon event

From Dev

Is it possible to set an Object to be null from within itself?

From Dev

Android Object Serializes itself from method

From Dev

Java converting an object to his superclass and then converting it back to itself keeps the reference

From Dev

Extract value from object if in object - reference error

From Dev

In Python, is object() equal to anything besides itself?

From Dev

Type of object class in python is defined inside itself?

From Dev

Python property access results in the property object itself

From Dev

Does python list store the object or the reference to the object?

From Dev

Does python list store the object or the reference to the object?

From Dev

Creating object from enum reference

From Dev

Get object reference from PropertyInfo

From Dev

Creating object from enum reference

From Dev

Remove reference from object javascript

From Dev

If a Javascript object has a property which holds a reference to a function, can I get that property name from within the function itself?

From Dev

Change representation of Python object

From Dev

Change representation of Python object

From Dev

Python - Change object shape?

Related Related

  1. 1

    c# Is it possible get a reference of an object, get the object itself and change it, instead of assinging to a new object?

  2. 2

    Java changes reference but not the object itself?

  3. 3

    Store the value of objects instead of a reference to the object itself

  4. 4

    Passing object reference as itself to own method explicitly

  5. 5

    Can a JavaScript object's child reference itself?

  6. 6

    Reference an object from within the object

  7. 7

    Python Object Reference

  8. 8

    Python multiprocessing object reference

  9. 9

    Python Object Reference

  10. 10

    how hashmap object reference change

  11. 11

    how hashmap object reference change

  12. 12

    Reference Object and change it upon event

  13. 13

    Is it possible to set an Object to be null from within itself?

  14. 14

    Android Object Serializes itself from method

  15. 15

    Java converting an object to his superclass and then converting it back to itself keeps the reference

  16. 16

    Extract value from object if in object - reference error

  17. 17

    In Python, is object() equal to anything besides itself?

  18. 18

    Type of object class in python is defined inside itself?

  19. 19

    Python property access results in the property object itself

  20. 20

    Does python list store the object or the reference to the object?

  21. 21

    Does python list store the object or the reference to the object?

  22. 22

    Creating object from enum reference

  23. 23

    Get object reference from PropertyInfo

  24. 24

    Creating object from enum reference

  25. 25

    Remove reference from object javascript

  26. 26

    If a Javascript object has a property which holds a reference to a function, can I get that property name from within the function itself?

  27. 27

    Change representation of Python object

  28. 28

    Change representation of Python object

  29. 29

    Python - Change object shape?

HotTag

Archive