Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class A:
- ......
- class B(A):
- .......
- aA = A()
- a = A()
- a.__class__ = B
- a = A()
- b = B()
- b.__dict__ = a.__dict__.copy()
- del a
- a = b
- del b
- >>> from collections import Counter
- >>> d = dict(a=1, b=0)
- >>> +Counter(d)
- Counter({'a': 1})
- 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) ')
- cA.pro = 7
- print(f'cA.pro: {cA.pro}')
- cA.pro: 7
Add Comment
Please, Sign In to add comment