Why does my __init__ function need to be #classmethod? - python

Here is a code snippet I wrote as a test. I noticed that if I don't define the init method as a classmethod, the code doesn't run:
class A(object):
def __init__(self):
self.value = 0
self.add(1)
#classmethod
def add(self, arg):
self.value += arg
class B(A):
#classmethod
def add(self, arg):
self.value += arg * 2
if __name__ == '__main__':
a = A()
b = B()
print a.value
print b.value
This outputs:
Traceback (most recent call last):
File "inherit.py", line 17, in <module>
a = A()
File "inherit.py", line 4, in __init__
self.add(1)
File "inherit.py", line 8, in add
self.value += arg
AttributeError: type object 'A' has no attribute 'value'
However if I change my init function to be #classmethod, the code works as intended:
class A(object):
#classmethod
def __init__(self):
self.value = 0
self.add(1)
#classmethod
def add(self, arg):
self.value += arg
class B(A):
#classmethod
def add(self, arg):
self.value += arg * 2
if __name__ == '__main__':
a = A()
b = B()
print a.value
print b.value
Output:
1
2
I was under the impression init was by default a class method whose first argument must be self. What is going on?

The problem is that you have add marked as a classmethod, but it isn't. Take out the #classmethod from the adds and it should work.

Related

How to dynamic create function with property decorator

In python, the #property and #val.setter is very helpful. For example:
from types import FunctionType
class Test:
def __init__(self):
self.a = 1
#property
def A(self):
print('get A')
return self.a
#A.setter
def A(self, val):
print('set A')
self.a = val
t = Test()
print(t.A)
t.A = 3
print(t.A)
It works.
Now, I want to create setProperty and getProperty for many variable, so I want to dynamic create those functions.
My code is:
from types import FunctionType
class Test:
def __init__(self):
self.a = 1
code = compile('#property\ndef A(self): print("get A")\nreturn self.a', '', 'exec')
FunctionType(code.co_consts[0], globals(), "A")
code = compile('#A.setter\ndef A(self, val): print("set A")\nself.a=val', '', 'exec')
FunctionType(code.co_consts[0], globals(), "A")
t = Test()
print(t.A)
t.A = 3
print(t.A)
And it reports a bug:
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/medpro/test.py", line 23, in <module>
t = Test()
File "C:/Users/Administrator/Desktop/medpro/test.py", line 7, in __init__
code = compile('#property\ndef A(self): print("get A")\nreturn self.a', '', 'exec')
File "", line 3
SyntaxError: 'return' outside function
Then, I remove print("get A"), and another bug is reported:
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/medpro/test.py", line 24, in <module>
print(t.A)
AttributeError: 'Test' object has no attribute 'A'
You can, but do not have to use property as decorator, consider following example from property docs
class C:
def __init__(self):
self._x = None
def getx(self):
return self._x
def setx(self, value):
self._x = value
def delx(self):
del self._x
x = property(getx, setx, delx, "I'm the 'x' property.")
To dynamically add descriptors, in this case read & write, you can first create a decorator which take as arguments the attributes identifiers and add them to your target class.
class DynamicDescriptors:
def __init__(self, id_attributes: tuple):
self.ids = id_attributes
def __call__(self, cls):
for attr in self.ids:
# getter
setattr(cls, attr, property(lambda cls_self: getattr(cls, attr)))
# setter
setattr(cls, attr, getattr(cls, attr).setter(lambda cls_self, v: setattr(cls, attr, v)))
return cls
dynamic_attrs = ('a', 'b', 'c')
#DynamicDescriptors(dynamic_attrs)
class Test:
pass
# access from instance
t = Test()
t.a = 'a'
print(t.a)
t.b = 'b'
print(t.b)
t.c = 'c'
print(t.c)
# access from class
Test.a = 10
print(Test.a)
# property object
print(Test.b)
Output
a
b
c
10
<property object at 0x7f5ff9d12c70>
EDIT (reimplementation) without decorator + support for print (or custom implementation)
There are a plenty of different ways to achieve the goal without decorator. I like to separate the tasks, so I add a classmethod which add the descriptors and the attributes are given as class attribute.
Note by personal choice I assign to a descriptor a a private name __a. Since adding the descriptor dynamically and since the private name mangling happens at compilation time the attribute __a should be called as _ClassName__a, see docs 1, 2. For non private name attribute no need for that.
class Test:
dynamic_attrs = ('a', 'b', 'c')
#classmethod
def dynamic_descriptors(cls, *id_attributes):
def prop_factory(attr):
def getter(attr):
def __wrapper(self):
p_attr = f'_{type(self).__name__}__{attr}' # private name
v_attr = getattr(self, p_attr)
print(f'getter {attr}: {v_attr}')
return v_attr
return __wrapper
def setter(attr):
def __wrapper(self, v):
p_attr = f'_{type(self).__name__}__{attr}' # private name
old_attr = getattr(self, p_attr) if hasattr(self, p_attr) else 'None'
setattr(self, p_attr, v)
print(f'setter {attr}: {old_attr} -> {v}')
return __wrapper
return property(getter(attr), setter(attr))
for attr in id_attributes:
setattr(cls, attr, prop_factory(attr))
def __init__(self):
self.dynamic_descriptors(*self.dynamic_attrs)
t = Test()
print(Test.a)
#<property object at 0x7f4962138cc0>
t.a = 'aaa'
#setter a: None -> aaa
t.a
#getter a: aaa
t.b = 'b'*4
#setter b: None -> bbbb
t.b = 'b'*2
#setter b: bbbb -> bb
t.b
#getter b: bb
t.c = 'c'
#setter c: None -> c

In this code while executing a=A() and b=B() what gets passed in obj argument?

class A:
x=1
def __add__(self, obj):
if isinstance(obj,A):
return self.x+obj.x
return "False"
class B(A):
x=2
a=A()
b=B()
print(a+b)
The add method takes self, the first object in the addition, and another one, other.
For example:
class A:
def __init__(self, x):
self.x=x
def __add__(self, obj):
if isinstance(obj,A):
return self.x+obj.x
raise NotImplementedError
a = A(3)
b = A(4)
res = a + b # this is essentially a.__add__(obj=b)
# self is implicitly the object a
# res == 7

Unable to Mock class that inherits from another class (which creates/uses singleton object)

I have three classes and one unittest case:
1) A.py:
class A(object):
def __new__(cls):
"""
Overriding the __new__ method to make the A a singleTon class
:return: cls.instance
"""
if not hasattr(cls, 'instance') or not cls.instance:
cls.instance = super(A, cls).__new__(cls)
return cls.instance
def execute():
print("class A")
return "Coming from class A"
2) B.py:
from A import A
class B(object):
def __init__(self):
"""
Initialize and declare any members/methods that can be passed over class that inherit this class
"""
self.a = A()
self.name = Name()
def run(self):
pass
class Name:
def __init__(self):
self.test_dict = {}
3) C.py
from B import B
class C(B):
def __init__(self):
super(C, self).__init__()
self.result = None
def run(self):
self.result = self.A.execute()
return self.result
4) test.py
import unittest
from unittest.mock import patch
from A import A
from B import B
from C import C
class TestMethods(unittest.TestCase):
#patch('A.A')
def test_basic(self, MockA):
a = MockA()
a.execute.return_value = "Testing code"
c = C()
result = c.run()
self.assertEqual(result,"Testing code")
if __name__ == '__main__':
unittest.main()
while executing test.py, I am getting following error:
ERROR: test_basic (main.TestMethods)
Traceback (most recent call last):
File "C:\Users\davinder\AppData\Local\Continuum\Anaconda3\lib\unittest\mock.py", line 1179, in patched
return func(*args, **keywargs)
File "C:/Users/davinder/PythonCodes/Test Framework/test.py", line 14, in test_basic
c = C()
File "C:\Users\davinder\PythonCodes\Test Framework\C.py", line 5, in __init__
super(C, self).__init__()
File "C:\Users\davinder\PythonCodes\Test Framework\B.py", line 8, in __init__
self.a = A()
File "C:\Users\davinder\PythonCodes\Test Framework\A.py", line 8, in __new__
cls.instance = super(A, cls).__new__(cls)
TypeError: super() argument 1 must be type, not MagicMock
----------------------------------------------------------------------
Ran 1 tests in 0.018s
FAILED (errors=1)
I want to test run() from class C.py by using different return value used from test.py(by patching).
Thanks for help in advance
Edit: Even if I mock C in test.py, like this:
import unittest
from unittest.mock import patch
from A import A
from B import B
from C import C
from B import Name
class TestMethods(unittest.TestCase):
#patch('C.C')
#patch('A.A')
def test_basic(self, MockA, MockC):
a1 = MockA()
a1.execute.return_value = "Testing code"
c = MockC()
c.a = a1
c.name = Name()
result = c.run()
self.assertEqual(result,"Testing code")
if __name__ == '__main__':
unittest.main()
getting this error:
FAIL: test_basic (__main__.TestMethods)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\davinder\AppData\Local\Continuum\Anaconda3\lib\unittest\mock.py", line 1179, in patched
return func(*args, **keywargs)
File "C:/Users/davinder/PythonCodes/Test Framework/test.py", line 22, in test_basic
self.assertEqual(result,"Testing code")
AssertionError: <MagicMock name='C().run()' id='2464384921272'> != 'Testing code'
----------------------------------------------------------------------
Ran 1 tests in 0.018s
FAILED (failures=1)
I had to change your import in B.py to be an import instead of a from. The error was because A was being looked up from the B module, so your patch would have to be #patch('B.A'). Here's a useful read anyway on that subject: http://www.voidspace.org.uk/python/mock/patch.html#where-to-patch
Now, here's the changed code:
"""test.py"""
import unittest
from unittest.mock import patch
from c_module import C
class TestMethods(unittest.TestCase):
#patch('b_module.A')
def test_basic(self, mock_a):
mock_a.return_value.execute.return_value = "Testing code"
c = C()
result = c.run()
self.assertEqual(result,"Testing code")
if __name__ == '__main__':
unittest.main()
"""c_module.py"""
from b_module import B
class C(B):
def __init__(self):
super(C, self).__init__()
self.result = None
def run(self):
self.result = self.a.execute()
return self.result
"""b_module.py"""
from a_module import A
class B(object):
def __init__(self):
"""
Initialize and declare any members/methods that can be passed over class that inherit this class
"""
self.a = A()
self.name = Name()
def run(self):
pass
class Name:
def __init__(self):
self.test_dict = {}
"""a_module.py"""
class A(object):
def __new__(cls):
"""
Overriding the __new__ method to make the A a singleTon class
:return: cls.instance
"""
if not hasattr(cls, 'instance') or not cls.instance:
cls.instance = super(A, cls).__new__(cls)
return cls.instance
def execute():
print("class A")
return "Coming from class A"
I'd also encourage you to change your module naming. While working on this, it's hard to not conflate the modules with the classes. You can see above I've changed the module names to be snake-cased and left the classes as-is.

Python: how to count the access of an instance variable

I have a python class as below.
class A(object):
def __init__(self, logger):
self.b = B()
self.logger = logger
def meth1(self):
self.b.mymethod1()
def meth2(self):
self.meth1()
self.b.mymethod2()
.........
class B(object):
---------
How can I count how many time I accessed self.b variable on the invocation of meth2() or any method of class A. Is there any way, I can log the usage of self.b variable?
make 'b' a property and and increase the counter corresponding to be in the setter.
#property
def b(self):
self.b_counter += 1
return self._b
and in your class replace b with _b
If you don't want to make a property, you can log the read/write access using __getattribute__ (not __getattr__ since b exists and would not be called) and __setattr__:
class A(object):
def __init__(self):
# initialize counters first !
self.b_read_counter = 0
self.b_write_counter = 0
# initialize b
self.b = 12
def __getattribute__(self,attrib):
# log read usage
if attrib=="b":
self.b_read_counter+=1
# now return b value
return object.__getattribute__(self, attrib)
def __setattr__(self,attrib,value):
if attrib=="b":
self.b_write_counter+=1
return object.__setattr__(self, attrib,value)
a = A()
a.b = 23 # second write access (first is in the init method)
if a.b == 34: # first read access
print("OK")
if a.b == 34:
print("OK")
if a.b == 34: # third read access
print("OK")
print(a.b_read_counter)
print(a.b_write_counter)
result:
3
2
You can use descriptors for this or just make a property which is basically is descriptor.
class A(object):
def __init__(self, logger):
self._b = B()
self._b_counter = 0
self.logger = logger
#property
def b(self):
self._b_counter += 1
return self._b
def meth1(self):
self.b.mymethod1()
def meth2(self):
self.meth1()
self.b.mymethod2()
You can use property, somtehing like:
class A(object):
def __init__(self, logger):
self._b = B()
self._count = 0
self.logger = logger
#property
def b(self):
self._count += 1
return self._b
...
...

Python : How to "merge" two class

I want to add some attributes and methods into various class. The methods and attributes that I have to add are the same but not the class to assign them, so I want to construct a class who assign new methods and attributes for a class given in argument.
I try this but it's not working:
(I know that is a very wrong way to try to assign something to self, it's just to show what I want to do)
class A:
def __init__(self):
self.a = 'a'
def getattA(self):
return self.a
class B:
def __init__(self, parent) :
self = parent
# This is working :
print self.getattA()
def getattB(self):
return self.getattA()
insta = A()
instb = B(insta)
# This is not working :
print instb.getattB()
The result is :
a
Traceback (most recent call last):
File "D:\Documents and settings\Bureau\merge.py", line 22, in <module>
print instb.getattB()
File "D:\Documents and settings\Bureau\merge.py", line 16, in getattB
return self.getattA()
AttributeError: B instance has no attribute 'getattA'
And I expected to got 'a' for the call of instb.gettattB()
To resume I want to inherit class B from class A giving class A in argument of class B because my class B will be a subclass of various class, not always A.
The Best answer is in the comments, it was useful for me so I decided to show it in an answer (thank to sr2222):
The way to dynamicaly declare inherance in Python is the type() built-in function.
For my example :
class A(object) :
def __init__(self, args):
self.a = 'a'
self.args = args
def getattA(self):
return self.a, self.args
class B(object) :
b = 'b'
def __init__(self, args) :
self.b_init = args
def getattB(self):
return self.b
C = type('C', (A,B), dict(c='c'))
instc = C('args')
print 'attributes :', instc.a, instc.args, instc.b, instc.c
print 'methodes :', instc.getattA(), instc.getattB()
print instc.b_init
The code return :
attributes : a args b c
methodes : ('a', 'args') b
Traceback (most recent call last):
File "D:\Documents and settings\Bureau\merge2.py", line 24, in <module>
print instc.b_init
AttributeError: 'C' object has no attribute 'b_init'
My class C inerhite attributes and methods of class A and class B and we add c attribute. With the instanciation of C (instc = C('args')) The init for A is call but not for B.
Very useful for me because I have to add some attributes and methodes (the same) on different class.
I was having trouble with calling different constructors, using super doesn't necessarily make sense in a case like this, I opted to inherit and call each constructor on the current object manually:
class Foo(object):
def __init__(self, foonum):
super(Foo, self).__init__()
self.foonum = foonum
class Bar(object):
def __init__(self, barnum):
super(Bar, self).__init__()
self.barnum = barnum
class DiamondProblem(Foo, Bar):
# Arg order don't matter, since we call the `__init__`'s ourself.
def __init__(self, barnum, mynum, foonum):
Foo.__init__(self, foonum)
Bar.__init__(self, barnum)
self.mynum = mynum
How about this?
class A:
def __init__(self):
self.a = 'a'
def getatt(self):
return self.a
class B:
def __init__(self, parent) :
self.parent = parent
def __getattr__(self, attr):
return getattr(self.parent, attr)
def getattB(self):
return self.parent.getatt()
insta = A()
instb = B(insta)
print instb.getattB()
print instb.getatt()
But method in class A can not access attr in class B.
Another way:
import functools
class A:
def __init__(self):
self.a = 'a'
def getatt(self):
return self.a
class B:
def __init__(self, parent):
for attr, val in parent.__dict__.iteritems():
if attr.startswith("__"): continue
self.__dict__[attr] = val
for attr, val in parent.__class__.__dict__.iteritems():
if attr.startswith("__"): continue
if not callable(val): continue
self.__dict__[attr] = functools.partial(val, self)
def getattB(self):
return self.getatt()
insta = A()
instb = B(insta)
print instb.__dict__
print instb.getattB()
print instb.getatt()
Slow with init but call fast.
Since B is not a subclass of A, there is no path in B to getatt() in A
I guess i have a easier method
class fruit1:
def __init__(self):
self.name = "apple"
self.color = "blue"
class fruit2:
def __init__(self):
self.name = "banana"
self.size = 100
def merge(ob1, ob2):
ob1.__dict__.update(ob2.__dict__)
return ob1
f1 = fruit1()
f2 = fruit2()
fruit = merge(f1, f2)
print("name:",fruit.name," color:",fruit.color, " size:",fruit.size)
#output: name: banana color: blue size: 100
I'm not certain what you are trying to do, but the code below is giving my the output I think you are expecting. notice:
a is initialized outside the constructor in A
B is declared as a subclass of A
Code:
class A:
a='' #Initialize a
def __init__(self):
self.a = 'a'
def getatt(self):
return self.a
class B(A): #Declare B as subclass
def __init__(self, parent) :
self = parent
print self.getatt()
def getattB(self):
return self.getatt()
insta = A()
instb = B(insta)
print instb.getattB()
Helper function below conducts the merge of the dataclass instances, the attributes orders is derived from *args order:
from dataclasses import dataclass
#dataclass
class A:
foo: str
bar: str
def merge_dataclasses(*args):
if len({e.__class__.__name__ for e in args}) > 1:
raise NotImplementedError('Merge of non-homogeneous entries no allowed.')
data = {}
for entry in args[::-1]:
data.update(vars(entry))
return entry.__class__(**data)
print(merge_dataclasses(A(foo='f', bar='bar'), A(foo='b_foo', bar='b_bar')))
One easy way to merge two or more classes is through the tool set dyndesign:
from dyndesign import mergeclasses
class Base:
def __init__(self, init_value):
self.param = init_value
def m1(self):
print(f"Method `m1` of class `Base`, and {self.param=}")
def m2(self):
print(f"Method `m2` of class `Base`")
class Ext:
def m1(self):
print(f"Method `m1` of class `Ext`, and {self.param=}")
MergedClass = mergeclasses(Base, Ext)
merged_instance = MergedClass("INITIAL VALUE")
merged_instance.m1()
# Method `m1` of class `Ext`, and self.param='INITIAL VALUE'
merged_instance.m2()
# Method `m2` of class `Base`
Emphasizing ThorSummoner's's answer and Hong's comment; this method appears to be cleaner than the excepted answer. Notice Hong's use of super().init(self) in all but the last object added to the merge class.
class Foo(object):
def __init__(self, foonum):
super(Foo, self).__init__(self)
self.foonum = foonum
class Bar(object):
def __init__(self, barnum):
super(Bar, self).__init__(self)
self.barnum = barnum
class Oops(object):
def __init__(self, oopsnum):
super(Oops, self).__init__()
self.oopsnum = oopsnum
class DiamondProblem(Foo, Bar, Oops):
def __init__(self, mynum, foonum, barnum, oopsnum):
Foo.__init__(self, foonum)
Bar.__init__(self, barnum)
Oops.__init__(self, oopsnum)
self.mynum = mynum
def main():
dia = DiamondProblem(1, 10, 20, 30)
print(f"mynum: {dia.mynum}")
print(f"foonum: {dia.foonum}")
print(f"barnum: {dia.barnum}")
print(f"oopsnum: {dia.oopsnum}")

Categories

Resources