I am given an object a of the class A. I do not understand how class A works. Furthermore an unknown number of methods throughout the module I am using use A. So for all practical purposes A is unknown and we can only manipulate an instance of it and one known method, method2.
I am given an instance a. I want to convert a to a class B such that it remains identical in every respect except that method2 (which was present in the original class A and prints a) now prints b. How do I modify the piece of code below to be able to do that ?
class B(A):
def __init__(self,**kwargs):
super().__init__(**kwargs)
def method2(self):
print('b')
a.method1() #prints '1'
a.method2() #prints 'a'
print(a[0]) #prints 1
#a = convertAtoB(a)
a.method1() #prints '1'
a.method2() #should print 'b'
print(a[0]) #prints 1
I am aware of a previous answer to a similar question which involved using __getattr__ however when trying the below piece of code:
class B(object):
def __init__(self, a):
self.__a = a
def __getattr__(self, attr):
return getattr(self.__a, attr)
def __setattr__(self, attr, val):
object.__setattr__(self, attr, val)
def method2(self):
print('b')
I got, in the practical problem I am having, the error TypeError: 'B' object is not subscriptable.
edit : added a subscript test, as I mentioned above, I don't entirely understand how A works or which methods in the imported module need A to work.
__setattr__()and__getattr__()don't match - you wouldn't be able to get a property you set via__setattr__()). The error'B' object is not subscriptableoccurs when you try to use square-bracket notation (e.g.B[2]) and the__getattribute__()method is not defined. Is there some code you haven't shown us where that's happening?Aclass in the first place? Also, where do you expect the data to come from for data that is part ofBbut not part ofA?