I have two python classes defined like follows :
class A(object) :
def __init__(self, param) :
print('A.__init__ called')
self.param = param
def __new__(cls, param) :
print('A.__new__ called')
x = object.__new__(A)
x._initialize() # initialization code
return x
class B(A) :
def __init__(self, different_param) :
print('B.__init__ called')
def __new__(cls, different_param) :
print('B.__new__ called')
# call __new__ of class A, passing "param" as parameter
# and get initialized instance of class B
# something like below
b = object.__new__(B)
param = cls.convert_param(different_param)
return super(B, cls).__new__(b, param) # I am expecting something
# similar to this
#staticmethod
def convert_param(param) :
return param
class B is a subclass of class A. The difference between the two classes is that the parameters passed to class B are in a different format as compared to those expected by class A. So, the convert_param method of classB is called to convert the parameters to be compatible with the __new__ method of class A.
I am stuck at the part where I wish to call the __new__ method of class A from the __new__ method of class B, since there is a lot of initialisation that takes place in there, and then get back the initialised instance of class B.
I am having a difficult time figuring this out. Please help.
convert_param should either be a staticmethod or a classmethod and you don't want to be calling object.__new__ from B (otherwise, you're essentially trying to create two new instances of B instead of just one). If convert_param is a staticmethod or a classmethod, then you can do the parameter conversion before you have an instance (e.g. before __new__ has been called on the superclass):
class B(A):
#staticmethod
def convert_param(params):
# do magic
return params
def __new__(cls, params):
params = cls.convert_params(params)
return super(B, cls).__new__(cls, params)
Additionally, you'll need to change A's __new__ slightly to not hard-code the type of the instance returned from object.__new__:
class A(object) :
def __new__(cls, param) :
print('A.__new__ called')
x = super(A, cls).__new__(cls)
x._initialize() # initialization code
return x
Related
Suppose I have:
class Super:
def __init__(self,a):
self.a = a
#classmethod
def from_b(cls,b):
return cls(b.to_a())
class Regular(Super):
def __init__(self,b):
# how to set my super to the output of
super = super.from_b(b)
How do I correctly initialize the super class with the output of the super class method rather than init?
My OOP background is in C++ and I am continually getting into these scenarios due to the ability to overload constructors in C++, so a workaround for this would be awesome.
#shx2's answer works but wastefully/awkwardly creates a throw-away Super object just to initialize the new Regular object with its a attribute.
If you have control over the source of Super, you can make the from_b method create an instance of the given subclass, and have the subclass call the from_b method in its __new__ method instead, so that a Regular object can be both created and initialized directly:
class Super:
def __init__(self, a):
self.a = a
#classmethod
def from_b(cls, b):
obj = super().__new__(cls)
cls.__init__(obj, b.to_a())
return obj
class Regular(Super):
def __new__(cls, b):
return super().from_b(b)
so that the following assertions will pass:
from unittest.mock import Mock
obj = Regular(Mock())
assert type(obj) is Regular
assert obj.a.to_a.is_called()
This is slightly awkward (since what you're trying to do is slightly awkward), but it would work:
class Super:
def __init__(self,a):
self.a = a
#classmethod
def from_b(cls,b):
return cls(b.to_a())
class Regular(Super):
def __init__(self,b):
a = Super.from_b(b).a
super().__init__(a)
By the way, it might help keeping in mind that a "constructor" method such as from_b() (typically) returns a new object, while __init__() only initializes an object after it's been created.
I built 2 classes. first class with its own init value and another class will inherit the init values from the first class. I am wondering whether I understand it correctly of the usage of super().
class testing(testing_2):
def __init__(self, name, c):
super().__init__(name, c)
def check(self):
print(super().name)
class testing_2:
def __init__(self, name, c):
self.name = name
self.c = c
tt = testing("tester", "check")
tt.check()
I thought my code was supposedly printed the "tester" because I initialize the testing class with name and c. since testing class inherit from testing_2 so we can just print the name. Am I confusing something?
My expectation is:
testing_2 will take the value from testing and we can print the values of testing_2 in testing.
Simplify it:
class A:
def __init__(self, name, c):
self.name = name
self.c = c
class B(A):
def check(self):
print(self.name)
tt = B("tester", "check")
tt.check()
The B object will have all the same things as an A object, since it inherits them. No need to implement __init__ on B if it doesn't do anything useful. You can access self.name directly, just as you would within A. The object that has that property is self. It's set on self and you can access it with self.
Remember, self is the object instance, not the class. When doing B(...), the self in A.__init__(self, ...) is actually an instance of B.
You only need to explicitly use super if you are overriding parent methods, like in:
def __init__(self, name, c):
super().__init__(name, c)
Here __init__ is overridden, and in order to execute the parent's __init__ you need to access it through super. Just self.__init__(name, c) would access the child's __init__ method and you'd call it in an endless recursive loop.
I would like to create a subclass instance from a superclass instance in Python. Suppose I have something like this:
class A():
def __init__(self, type):
...
self.type = type # this will be something that corresponds to either B or C
class B(A):
def do_something():
# this method is subclass specific
class C(A):
def do_something():
# this method is again subclass specific
I have a function that receives an instance of A, and I need to create an instance of either B or C (or D ...) based on what A's attribute type is.
I'm not sure how to go about this. Is there a way out of this or does the solution need to be redesigned?
Thank you
Start by redefining the classes A, B and C as follows. Note that you also need to pass the type value from subclass to superclass constructor via super().__init__()
class A():
def __init__(self, type):
...
self.type = type # this will be something that corresponds to either B or C
class B:
def __init__(self, type):
super().__init__(type)
def do_something(self):
print('do_something called for B')
class C:
def __init__(self, type):
super().__init__(type)
def do_something(self):
print('do_something called for C')
Then make another class which can make the decision whether to call B and C for you, and save that object locally
class User:
def __init__(self, type):
self.obj = None
if type == 'B':
self.obj = B(type)
elif type == 'C':
self.obj = C(type)
Then you can instantiate user class with different types and see that the correct do_something is called.
user_B = User('B')
user_B.obj.do_something()
#do_something called for B
user_C = User('C')
user_C.obj.do_something()
#do_something called for C
Use a dictionary that maps from types to classes.
class A():
typemap = {}
def __init__(self, typearg): # renamed this argument so it doesn't shadow standard type() function
self.type = typearg
self.typemap[typearg] = type(self)
def create_child(self, *args):
return typemap[self.type](*args)
When the constructor runs, type(self) gets the subclass of the object being created. This is then stored in the dictionary, so we can look it up using self.type.
The create_child() looks up the class in the dictionary, and calls it to create a new instance of that child class.
I want to do something like:
class A(Resource):
#dec(from_file=A.docpath)
def get(self):
pass
class B(A):
docpath = './docs/doc_for_get_b.json'
class C(A):
docpath = './docs/doc_for_get_c.json'
def dec(*args, **kwargs):
def inner(f):
docpath = kwargs.get('from_file')
f.__kwargs__ = open(path, 'r').read()
return f
return inner
The functions that will be called are B.get and C.get, never A.get.
How can I access the custom attribute docpath defined in class B or class C and pass it to the decorator of the get function in class A ?
Current solution: Put the decorator on each derived class ...
class A(Resource):
def _get(self):
pass
class B(A):
#dec(from_file='./docs/doc_for_get_b.json')
def get(self):
return self._get()
class C(A)
#dec(from_file='./docs/doc_for_get_c.json')
def get(self):
return self._get()
This works but it's pretty ugly compared to the one-line declaration of the classes in the previous code.
To access a class's attributes inside the decorator is easy:
def decorator(function):
def inner(self):
self_type = type(self)
# self_type is now the class of the instance of the method that this
# decorator is wrapping
print('The class attribute docpath is %r' % self_type.docpath)
# need to pass self through because at the point function is
# decorated it has not been bound to an instance, and so it is just a
# normal function which takes self as the first argument.
function(self)
return inner
class A:
docpath = "A's docpath"
#decorator
def a_method(self):
print('a_method')
class B(A):
docpath = "B's docpath"
a = A()
a.a_method()
b = B()
b.a_method()
In general I've found using multiple levels of decorators, i.e. decorator factory functions that create decorators such as you've used and such as:
def decorator_factory(**kwargs):
def decorator_function(function):
def wrapper(self):
print('Wrapping function %s with kwargs %s' % (function.__name__, kwargs))
function(self)
return wrapper
return decorator_function
class A:
#decorator_factory(a=2, b=3)
def do_something(self):
print('do_something')
a = A()
a.do_something()
a difficult thing to get right and not easy to comprehend when reading code, so I would err towards using class attributes and generic superclass methods in favour of lots of decorators.
So in your case, don't pass the file path in as an argument to your decorator factory, but set it as a class attribute on your derived classes, and then write a generic method in your superclass that reads the class attribute from the instance's class.
class Process(object):
def __init__(self, obj, callback):
if obj and hasattr(callback, 'im_self') and callback.im_self is obj:
self.obj = obj
self.callback=callback.im_func.__name__
else:
raise Exception('invalid callback')
class A(object):
def parse(self):
print 'in parse()'
return Process(self, callback=self.test)
def test(self):
print 'in class A'
class B(A):
def test(self):
print 'in class B'
# do something...
return Process(self, callback=super(B, self).test)
b=B()
p=b.parse()
callback = getattr(p.obj, str(p.callback))
p=callback()
callback = getattr(p.obj, str(p.callback))
callback()
output of the program:
in parse()
in class B
in class B
doesn't call A.test()
I am using Scrapy, it use this way in Process() to save callback function. How to call A.test() through B.test()?
Update due to comments
You are right. Both A and B are spider, A is a base spider, B has a special procedure, so want to call A.test() after B.test(). Have to change class B, it difficult to modify Scrapy’s source code.
class B(A):
def test_again(self):
return super(B,self).test()
def test(self):
print 'in class B'
return Process(self, callback=self.test_again)
You can't, not by only storing the name of the function. There isn't enough context there to distinguish between the original method and the override.
You'd either need to store the original method (e.g. not reach in and grab the original function) or pass in a different method that uses super() to call the original:
def original_test(self):
return super(B, self).test()
super() isn't limited to just the current method, after all.
Note that the method object .__name__ attribute will always return the wrapped function object .__name__ attribute, no need to unwrap the method just to get to that value.