How to find an attribute in self.__init__? - python

class C:
def __init__(self):
self.a = 1
self.b = 2
def __setattr__(self,name,value):
if a in self.__init__: #Determine if a is an instance of __init__ function
do something
The above code will return an error and says
if name in self.__init__:
TypeError: argument of type 'method' is not iterable
If I don't iterate through self.__init__ function, how else am I supposed to know what attributes are defined in self.__init__ function?
If an attribute is set in init, I want to set the name prefixed by "somestring_" and append it to self__dict__: e.g., if I print self.__dict__ after self.__setattr__, it will print {'somestring_a': 1, 'somestring_b': 2}

Add an attribute that lists the attributes that are set in __init__, and use that.
class C:
predefined = ['a', 'b']
def __init__(self):
self.a = 1
self.b = 2
def __setattr__(self,name,value):
if name in self.predefined:
do something
else:
do something else
Another option would be to copy the keys of self.__dict__ at the end of the __init__ method:
class C:
def __init__(self):
self.a = 1
self.b = 2
self.predefined = set(self.__dict__)
def __setattr__(self,name,value):
if name in self.predefined:
do something
else:
do something else

class C():
def __init__(self, a, b):
self.a = a
self.b = b
a = C(1, 2)
print(a.__dict__)
>>> {'a': 1, 'b': 2}
So __dict__.keys() will give you the list of attributes ...
BUT :::
if you will check the list of your attributes in __setattr__ , you have to keep in mind that this function is also called when you do a = C(1, 2) so you shouldn't check your attributes in this level of code.

Related

Access all variables by their names of specific object in class

I have two python classes:
class A:
def __init__(self, param1):
self.param1 = param1
class B:
def __init__(self, a):
self.a = a
Now I have an instance of B and need to access param1, I can just write b.a.param1. But the goal is to omit the 'a' part, so access this param with b.param1. I could add property to class B, but I am searching for generic solution - when A class has a lot variables. Is it possible? And would it be clean solution?
This is not the most elegant option and probably a bad practice but you can copy all the attributes of a to be attributes of b using getattr and setattr:
import inspect
class A:
def __init__(self, param1):
self.param1 = param1
class B:
def __init__(self, a):
self.a = a
variables = [i for i in dir(a) if not inspect.ismethod(i) and i[:2] != '__']
for var in variables:
setattr(self, var, getattr(a, var))
This way you can access a's attributes directly:
a = A(1)
b = B(a)
b.param1
which will return
1

How to access objects from a different class?

There are three classes :
A, B and C
The __init__ of B creates an object of A. Using the mutators, I will be able to change the attributes of A from B for the instance created.
However, I am not unable to find any way to use that instance of A created by B to be used in C without passing the Object explicitly to the __init__ method [ not C.__init(self, object: A) ]
Is there any way to implicitly allow C to use that instance of A ?
I am new to python and not sure if this a valid question. I have looked at other sources where it explicitly passes the object to class C
class A:
def __init__(self):
x = []
y = []
class C :
def __init__(self):
#[get obj1 without passing the instance in init]
self.value = None
def method1():
self.value = len([]) #len(obj1 of A.x)
class B:
def __init__(self):
obj1 = A()
obj1.x = [1,2,3,4]
obj1.y = [1,2,3]
obj2 = B()
print(obj2.value) #this should be the length of x in the instance A created above
Here is a simple example:
class A:
def __init__(self, i = ""):
self.item = i
class B:
def __init__(self):
self.a = A("hello")
class C:
def __init__(self):
b = B()
print(b.a.item)
c = C()
Output:
hello
Let's say we have classes A and B:
class A:
def hello_world(self):
print("hello world")
class B:
def __init__(self):
self.a = A()
def hello_world(self):
self.a.hello_world()
You create an instance of class B (which will create an instance of class A inside):
b = B()
You can then pass a reference to either b or b.a to any function of an instance of class C (either a constructor or not)
class C:
def hello_world(self, a):
a.hello_world()
c = C()
c.hello_world(b.a)
You can also use global variables:
class C:
def hello_world(self):
b.a.hello_world()
c = C()
c.hello_world()
Here the instances of class C will rely on variable b to be in place and just use its a attribute.
Using global variables in classes is generally considered to be hard to maintain and a bad practice. If your class depends on a value or an instance of some class you should pass the reference in the constructor (__init__ function) or in the function that's using it.
If these classes are in different different python files then you can also use these classes by importing the class name and creating an object of that class:
eg:
file1.py
class A:
def __init__(self):
x = []
y = []
file2.py
from file1 import A
class C :
def __init__(self):
[get obj1 without passing the instance in init]
self.value = None
self.obj_a = A()
def xyz(self):
print "in class c"
file3.py
from file2 import C
from file1 import A
Class B:
def __init__(self):
self.obj_a = A()
self.obj_c = C()
def another_func(self):
print self.obj_c.xyz()# it will print "in class c"

Python shorthand for a tuple of a set of class attributes

Say i am using a class from some python package that looks like the following
class foo(object):
def __init__(self):
self.a = None
self.b = None
self.c = None
self.d = None
self.e = None
self.f = None
Now I need to use attributes b, d, and e of object foobar of class foo in some operation, say call a function qux for instance:
print qux(foobar.b, foobar.d, foobar.e)
Is there any way to create a shorthand version of this, something like the following imagined code:
print qux(*foobar.[b,d,e])
Note the constraints: neither the class nor the function can be changed.
Well, getattr and setattr get you close:
Assignment with setattr (not needed for the next to work, just here for illustration):
class foo(object):
def __init__(self):
for name in 'abcdef':
setattr(self, name, None)
Using values with getattr:
print qux(*(getattr(foobar, name) for name in 'bde'))
With normal, longer names you'd need to do in ['foo', 'bar'] instead.
Since you can't modify the class, how about a function that takes an instance and any number of attribute names, and returns a tuple:
class Foo(object):
def __init__(self):
self.a = 1
self.b = 2
self.c = 3
def getitems(obj, *items):
values = []
for item in items:
values.append(getattr(obj, item))
return tuple(values)
f = Foo()
print getitems(f, 'a', 'c') # prints (1, 3)
qux(*getitems(f, 'a', 'c'))
If you are willing to modify the class, you can override __getitem__ to accept a tuple of keys.
class Foo(object):
def __init__(self):
self.a = 1
self.b = 2
self.c = 3
def __getitem__(self, item):
if isinstance(item, basestring):
# treat single key as list of length one
item = [item]
values = []
for key in item:
# iterate through all keys in item
values.append(getattr(self, key))
return tuple(values)
f = Foo()
print f['a', 'c'] # prints (1, 3)
qux(*f['a', 'c'])

Python - how to get class instance reference from an attribute class?

class A()
att = B()
class B()
...
a = A()
b = B()
a.att = b
How can b get reference of a ? I need to get an attribute of a here.
Thanks!
You can make a generic "Reference()" class, that keep any reference of itself in an attributes dictionnary.
class Reference(object):
def __init__(self):
self.references = {}
def __setattr__(self, key, value):
if hasattr(self, 'references'):
if isinstance(value, Reference):
if not key in value.references:
value.references[key] = []
value.references[key].append(self)
elif value is None and hasattr(self, key):
old = getattr(self, key).references
if key in old and self in old[key]:
old[key].remove(self)
super(Reference, self).__setattr__(key, value)
And then, create your classes :
class A(Reference):
def __init__(self):
super(A, self).__init__()
self.att = None
class B(Reference):
def __init__(self):
super(B, self).__init__()
self.att = None
And use it :
a = A()
b = B()
print 'A references', a.references
print 'B references', b.references
# A references {}
# B references {}
a.att = b
print 'A references', a.references
print 'B references', b.references
# A references {}
# B references {'att': [<__main__.A object at 0x7f731c8fc910>]}
At the end, you'll have back reference to all Reference class from any properties
Easiest way would be to just add an extra function parameter to the method in B that needs A, and pass it through when called. Or, just make B's init take an A as argument, and change the bit in A's init to be att = B(self)
class A(object):
def __init__(self):
self.att = B(self)
class B(object):
def __init__(self, a):
self.a = a
a = A()
a.att.a is a
Or another way,
class A(object):
def __init__(self, b):
b.a = self
self.att = b
class B(object):
pass
a = A(B())
a.att.a is a
This code doesn't make a lot of sense... but if I correctly understand your question...
class A(object):
pass #or whatever you like
class B(object):
def __init__(self, ref): #accept one argument
self.ref = ref
a = A()
b = B(a) #pass `a` as that argument
a.att = b
Might be one answer.
class A(object):
def __init__(self):
self._att=None
#property
def att(self):
return self._att
#att.setter
def att(self, value):
self._att = value
value.parent = self
class B(object):
pass
a = A()
b = B()
a.att = b
print b.parent

Python object conversion

Assume that we have an object k of type class A. We defined a second class B(A). What is the best practice to "convert" object k to class B and preserve all data in k?
This does the "class conversion" but it is subject to collateral damage. Creating another object and replacing its __dict__ as BrainCore posted would be safer - but this code does what you asked, with no new object being created.
class A(object):
pass
class B(A):
def __add__(self, other):
return self.value + other
a = A()
a.value = 5
a.__class__ = B
print a + 10
a = A() # parent class
b = B() # subclass
b.value = 3 # random setting of values
a.__dict__ = b.__dict__ # give object a b's values
# now proceed to use object a
Would this satisfy your use case? Note: Only the instance variables of b will be accessible from object a, not class B's class variables. Also, modifying variables in a will modify the variable in b, unless you do a deepcopy:
import copy
a.__dict__ = copy.deepcopy(b.__dict__)
class A:
def __init__(self, a, b):
self.a = a
self.b = b
class B(A):
def __init__(self, parent_instance, c):
# initiate the parent class with all the arguments coming from
# parent class __dict__
super().__init__(*tuple(parent_instance.__dict__.values()))
self.c = c
a_instance = A(1, 2)
b_instance = B(a_instance, 7)
print(b_instance.a + b_instance.b + b_instance.c)
>> 10
Or you could have a sperate function for this:
def class_converter(convert_to, parent_instance):
return convert_to(*tuple(parent_instance.__dict__.values()))
class B(A):
def __init__(self, *args):
super().__init__(*args)
self.c = 5
But using the 2nd method, I wasn't able to figure out how to pass additional values

Categories