Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. class A(object):
  2. pass
  3.  
  4. class B(A):
  5. def __add__(self, other):
  6. return self.value + other
  7.  
  8.  
  9. a = A()
  10. a.value = 5
  11.  
  12. a.__class__ = B
  13.  
  14. print a + 10
  15.  
  16. a = A() # parent class
  17. b = B() # subclass
  18. b.value = 3 # random setting of values
  19.  
  20. a.__dict__ = b.__dict__ # give object a b's values
  21.  
  22. # now proceed to use object a
  23.  
  24. import copy
  25. a.__dict__ = copy.deepcopy(b.__dict__)
  26.  
  27. class A:
  28. def __init__(self, a, b):
  29. self.a = a
  30. self.b = b
  31.  
  32. class B(A):
  33. def __init__(self, parent_instance, c):
  34. # initiate the parent class with all the arguments coming from
  35. # parent class __dict__
  36. super().__init__(*tuple(parent_instance.__dict__.values()))
  37. self.c = c
  38. seld.d = d
  39.  
  40. a_instance = A(1, 2)
  41. b_instance = B(a_instance, 7)
  42. print(b_instance.a + b_instance.b + b_instance.c)
  43. >> 10
  44.  
  45. def class_converter(convert_to, parent_instance):
  46. return convert_to(*tuple(parent_instance.__dict__.values()))
  47.  
  48. class B(A):
  49. def __init__(self, *args):
  50. super().__init__(*args)
  51. self.c = 5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement