There is a program on Python 3:
class A: ...... class B(A): ....... aA = A() How can one correctly and beautifully convert aA (an instance of class A) into an instance of class B?
There is a program on Python 3:
class A: ...... class B(A): ....... aA = A() How can one correctly and beautifully convert aA (an instance of class A) into an instance of class B?
There is no general solution, you need to look and select the appropriate option for every A and B.
The main methods are:
one). Just replace the object class.
a = A() a.__class__ = B after such a procedure, the object will retain all its own attributes, and the parent’s attributes and methods will be addressed to B, not A. And when explicitly checking the class, it will be considered an instance of class B.
2). Create an instance of class B and throw all the attributes of the original object into it. Then use the new object. If you wish, you can give him the name of the old.
a = A() b = B() b.__dict__ = a.__dict__.copy() del a a = b del b Both of these options are not suitable for each case (for example, if the classes are different and non-trivial initializers). And in difficult cases, there is a high probability of something missing from attention and getting a poorly caught bug.
3). As it was rightly said in the comments, there is a slightly more complicated, but also more reliable way - to write logic in the initializer that could accept an object of another similar class as an alternative to the usual argument list, and create an instance of it in its image.
No Imagine A=object , and the implementation of B in no way limited. In general, having object() not enough to create a B() .
Specific classes B can, of course, define constructors that A accepts. For example, collections.Counter can be created from a dictionary ( dict ):
>>> from collections import Counter >>> d = dict(a=1, b=0) >>> +Counter(d) Counter({'a': 1}) In order to make an object of another class from an object of one class, you will have to add methods and change the type of the class. How to replace the type - they told you.
In one of three ways, you can add methods to an instance of a class :
class A: def back_print(a: str) -> None: print(a[::-1]) class B(A): def foo(self,a,b): print(a+b) b = B() print(type(b)) # строим функцию, которая будет добавлена как метод "а-ля" класс B() def foo(self,a,b): print(a, '+', b) aA = A() aA.foo = foo.__get__(aA) aA.foo(2,3) print(type(aA)) # подменяем тип: aA.__class__ = type(b) if type(aA)==type(b): print(' type(aA)==type(b) ') def bind(instance, method): def binding_scope_fn(*args, **kwargs): return method(instance, *args, **kwargs) return binding_scope_fn bA = A() bA.foo = bind(bA, foo) aA.foo(2,3) print(type(bA)) import types cA = A() cA.foo = types.MethodType(foo, cA) cA.foo(2,3) print(type(cA)) cA.__class__ = type(b) cA.foo(2,3) b.foo(2,3) if type(cA)==type(b): print(' type(cA)==type(b) ') How to add properties to a class can be read here in curved Russian:
I think that encapsulated properties are just “special methods” that can be added in the same way. Let the "fathers" correct me if I am wrong.
cA.pro = 7 print(f'cA.pro: {cA.pro}') you will get:
cA.pro: 7 In conclusion, how to add dynamically methods to the class. So now you have everything for "partisan patching" on the fly! Good luck.
Source: https://ru.stackoverflow.com/questions/773841/
All Articles