Can a class object know its "parent"? [duplicate] - python

In Python, is it possible to get the object, say Foo, that contains another object, Bar, from within Bar itself? Here is an example of what I mean
class Foo(object):
def __init__(self):
self.bar = Bar()
self.text = "Hello World"
class Bar(object):
def __init__(self):
self.newText = foo.text #This is what I want to do,
#access the properties of the container object
foo = Foo()
Is this possible? Thanks!

Pass a reference to the Bar object, like so:
class Foo(object):
def __init__(self):
self.text = "Hello World" # has to be created first, so Bar.__init__ can reference it
self.bar = Bar(self)
class Bar(object):
def __init__(self, parent):
self.parent = parent
self.newText = parent.text
foo = Foo()
Edit: as pointed out by #thomleo, this can cause problems with garbage collection. The suggested solution is laid out at http://eli.thegreenplace.net/2009/06/12/safely-using-destructors-in-python/ and looks like
import weakref
class Foo(object):
def __init__(self):
self.text = "Hello World"
self.bar = Bar(self)
class Bar(object):
def __init__(self, parent):
self.parent = weakref.ref(parent) # <= garbage-collector safe!
self.newText = parent.text
foo = Foo()

is it possible to get the object, say Foo, that contains another object, Bar, from within Bar itself?
Not "automatically", because the language isn't built like that, and in particular, the language is built such that there is no way to guarantee that Foo exists.
That said, you can always do it explicitly. Attributes, like every other identifier in Python, are just names, not storage space for data; so nothing prevents you from letting the Bar instance have a manually assigned foo attribute that is a Foo instance, and vice-versa at the same time.

Yes, it's possible. Even without passing the container reference on object creation, i.e. if your object is a class attribute.
Your object needs to implement the descriptor protocol (have a __get__()):
class ChildName(SimpleNamespace):
def __get__(self, instance, owner):
# instance is our parent
return f'I am {self.name}, my parent is {instance.name}.'
class ChildDiff(SimpleNamespace):
#property
def diff(self):
return self.born - self.parent.born
def age_diff(self):
return f'I am {self.diff} years older than {self.parent.name}.'
def __get__(self, instance, owner):
self.parent = instance # XXX: weakref?
return self # expose object to be able call age_diff() etc.
class Parent(SimpleNamespace):
child_name = ChildName(name='Bar')
child_diff = ChildDiff(born=42)
parent = Parent(name='Foo', born=23)
print(parent.child_name) # ... I am Bar, my parent is Foo.
print(parent.child_diff.age_diff()) # ... I am 19 years older than Foo.

This worked for me:
the parent:
import child
obj_1 = 25 # an object that both parent and child can access
def startup(): # any startup function
global obj_1
child.ref( obj_1 ) # send the shared object to the child
...
the child:
obj_1 = 0 # initual value will be overwritten
def ref(shared_obj): # receive a reference to the shared object
global obj_1 # shared object is global in the child, optional
obj_1 = shared_obj # set obj_1 in the child to be obj_1 in the parent

What about using inheritance:
class Bar(object):
def __init__(self):
self.newText = self.text
class Foo(Bar):
def __init__(self):
self.txt = 'Hello World'
Bar.__init__(self)
foo = Foo()
print foo.newText

Related

Get mangled attribute value of a parent class outside of a class

Imagine a parent class which has a mangled attribute, and a child class:
class Foo:
def __init__(self):
self.__is_init = False
async def init(self):
# Some custom logic here, not important
self.__is_init = True
class Bar(Foo):
...
# Create class instance.
bar = Bar()
# How access `__is_init` of the parent class from the child instance?
How can I get a __is_init value from a parent (Foo) class?
Obviously, I can bar._Foo__is_init in this example, but the problem is that class name is dynamic and I need a general purpose solution that will work with any passed class name.
The solution I see now is iterating over parent classes, and building a mangled attribute name dynamically:
from contextlib import suppress
class MangledAttributeError(Exception):
...
def getattr_mangled(object_: object, name: str) -> str:
for cls_ in getattr(object_, "__mro__", None) or object_.__class__.__mro__:
with suppress(AttributeError):
return getattr(object_, f"_{cls_.__name__}{name}")
raise MangledAttributeError(f"{type(object_).__name__} object has no attribute '{name}'")
Checking that this works:
class Foo:
def __init__(self):
self.__is_init = False
async def init(self):
self.__is_init = True
class Bar(Foo):
def __init__(self):
super().__init__()
bar = Bar()
is_init = getattr_mangled(bar, "__is_init")
print(f"is_init: {is_init}") # Will print `False` which is a correct value in this example
class Foo:
def __init__(self):
self.__is_init = False
async def init(self):
self.__is_init = True
class Bar(Foo):
def getattr_mangled(self, attr:str):
for i in self.__dict__.keys():
if attr in i:
return getattr(self,i)
# return self.__dict__[i] #or like this
bar = Bar()
print(bar.getattr_mangled('__is_init')) #False
if there is a need in __init__ in Bar we should of course initiate Foo's init too by: super().__init__()
When Foo's init is run, self namespace already has attribute name we need in the form we need it (like_PARENT_CLASS_NAME__attrname).
And we can just get it from self namespace without even knowing what parent class name is.

Getting NoneType return value when calling a parent class method using child class instance object

I wrote a class with methods that overwrites some methods of a parent class only when I want them to overwrite. Other times I call super of that method so that only things written in parent class method should execute. I observe that this works when I store data but not when I retrieve that data. A simplified take that shows the exact problem:
# parent class
class A(object):
def __init__(self):
self.var = {}
def assigner_method(self, value):
self.var = value
def returning_method(self):
return self.var
# child class
class B(A):
def returning_method(self):
#Do nothing
super(B, self).returning_method()
# What obviously works
class C(object):
def some_method(self):
self.obj = A()
self.obj.assigner_method("ABCD")
resp = self.obj.returning_method()
print resp
# What doesn't work:
class D(object):
def some_method(self):
self.obj2 = B()
self.obj2.assigner_method("ABCD")
resp = self.obj2.returning_method()
print resp
Now, this works:
print C().some_method()
ABCD
And this fails:
print D().some_method()
None
Putting some prints here and there, I see that setting the data self.var using self.obj2 works. Also when fetching data using self.obj2, the parent class returning_method prints returning data ABCD but when print at the caller, it says data received is NoneType. I think I did some fundamentally wrong here. Any help appreciated.

How to fetch class instance from class variable (from outside the class)?

Let's say I have this:
from PySide2 import QtWidgets
class MyClass(object):
def __init__(self, parent=None):
self.class_variable = QtWidgets.QWidget()
class_instance = MyClass()
variable = class_instance.class_variable
class_instance_returned = mystery_method(variable) # How to make this return class_instance?
How should I define mystery_method so that it would return the class_instance instance?
The real-world case I have is that I'm sending a QWidget which I'm using as a base instance for .ui file loading into a function. Inside this function I need to figure out which class instance it belongs to.
Python 2.7
class MyClass(object):
def foo():
return 'bar'
instance = MyClass()
def mystery_method(method):
return method.im_self.__class__
print mystery_method(instance.foo)
Python 3
class MyClass(object):
def foo():
return 'bar'
instance = MyClass()
def mystery_method(method):
return method.__self__.__class__
print mystery_method(instance.foo)
EDIT
After the OP was edited:
class ParentClass():
def foo():
return 'bar'
class MyClass(object):
def __init__(self, parent=None):
self.instance_attribute = ParentClass()
def mystery_method(method):
return method.__class__
class_instance = MyClass()
print mystery_method(class_instance.instance_attribute)
One way would we to define foo as a custom property that returns both its value and the related instance when its value is fetched:
from collections import namedtuple
class Prop(object):
def __init__(self, val):
self.val = val
def __get__(self, instance, type):
return namedtuple('Prop', ('value', 'instance'))(self.val, instance)
def __set__(self, instance, val):
self.val = val
class MyClass(object):
foo = Prop('bar')
Now in your program you can explicitly use its value and the related instance using foo's value and instance attributes respectively.
Demo:
>>> instance = MyClass()
>>> instance.foo
Prop(value='bar', instance=<__main__.MyClass object at 0x10effbcd0>)
>>> instance.foo.value
'bar'
>>> instance.foo.instance
<__main__.MyClass object at 0x10effbcd0>
In general you cannot (at least not without a lot of searching through all the objects in the system) but if all you want is to find which instances of a class match a particular value then it's fairly easy.
You can create a set of all instances and iterate over them to find what you need.
from weakref import WeakSet
class MyClass(object):
_instances = WeakSet()
def __init__(self, foo):
self._instances.add(self)
self.foo = foo
#classmethod
def findFoo(cls, foo):
return [instance for instance in cls._instances if instance.foo == foo]
>>> instance1 = MyClass('bar')
>>> instance2 = MyClass('baz')
>>> MyClass.findFoo('baz')
[<__main__.MyClass object at 0x7f6723308f50>]
>>> MyClass.findFoo('bar')
[<__main__.MyClass object at 0x7f6723308c50>]
Note that deleting the object won't remove it immediately, it may not go until garbage collected:
>>> del instance1
>>> MyClass.findFoo('bar')
[<__main__.MyClass object at 0x7f6723308c50>]
>>> import gc
>>> gc.collect()
16
>>> MyClass.findFoo('bar')
[]
However in general you would be better to keep the reference to the original object hanging around and just use that.
Also, note that you cannot reliably tell which instance holds 'bar' if it is stored in more than one object: they could be the same 'bar' or they could be different ones, and whether they are the same or different is an implementation detail.

Pass a parent class as an argument?

Is it possible to leave a parent class unspecified until an instance is created?
e.g. something like this:
class SomeParentClass:
# something
class Child(unspecifiedParentClass):
# something
instance = Child(SomeParentClass)
This obviously does not work. But is it possible to do this somehow?
You can change the class of an instance in the class' __init__() method:
class Child(object):
def __init__(self, baseclass):
self.__class__ = type(self.__class__.__name__,
(baseclass, object),
dict(self.__class__.__dict__))
super(self.__class__, self).__init__()
print 'initializing Child instance'
# continue with Child class' initialization...
class SomeParentClass(object):
def __init__(self):
print 'initializing SomeParentClass instance'
def hello(self):
print 'in SomeParentClass.hello()'
c = Child(SomeParentClass)
c.hello()
Output:
initializing SomeParentClass instance
initializing Child instance
in SomeParentClass.hello()
Have you tried something like this?
class SomeParentClass(object):
# ...
pass
def Child(parent):
class Child(parent):
# ...
pass
return Child()
instance = Child(SomeParentClass)
In Python 2.x, also be sure to include object as the parent class's superclass, to use new-style classes.
You can dynamically change base classes at runtime. Such as:
class SomeParentClass:
# something
class Child():
# something
def change_base_clase(base_class):
return type('Child', (base_class, object), dict(Child.__dict__))()
instance = change_base_clase(SomeParentClass)
For example:
class Base_1:
def hello(self):
print('hello_1')
class Base_2:
def hello(self):
print('hello_2')
class Child:pass
def add_base(base):
return type('Child', (base, object), dict(Child.__dict__))()
# if you want change the Child class, just:
def change_base(base):
global Child
Child = type('Child', (base, object), dict(Child.__dict__))
def main():
c1 = add_base(Base_1)
c2 = add_base(Base_2)
c1.hello()
c2.hello()
main()
Result:
hello_1
hello_2
Works well in both python 2 and 3.
For more information, see the related question How to dynamically change base class of instances at runtime?

access calling classes atrributes

I have 2 classes defined as such
class class1():
self.stuff = 1
def blah(self):
foo = class2()
foo.start()
class class2(threading.Thread):
def run(self):
#access class1.stuff
How would I access class1.stuff from class2
It could look like this:
class class1(object):
stuff = 1
def blah(self):
foo = class2()
foo.start()
class class2(threading.Thread):
def run(self):
print(class1.stuff)
There is no special syntax to "access calling classes atrributes". If you want access to a object you must give it a visible name, for example by passing it to __init__ or by using the class object like this.
You would have to pass it into the function.
class class1():
self.stuff = 1
def blah(self):
foo = class2()
foo.start(self)
class class2(threading.Thread):
def run(self, obj):
obj.stuff;
There is no way to access another object's properties without having a reference to the object. The easiest way to obtain a reference to an object of class1 is to ask for it as an argument.

Categories