1

I have an object which is instantiated from parent class(it will b variable at below example code) and i want to use this object like a child class instance without knowledge about the member variable of parent class

is there any recommendation?

class A:
    def __init__(self):
        pass # some member variables are assigned
    pass

class B(A):
    def test(self):
        print("test")
    pass

b = A()
b.test() # error
usan
  • 81
  • 1
  • 8
  • What is your use case? – Brian Destura Jun 01 '21 at 02:03
  • @bdbd thank you for your reply. actually i use pyvisa library. i want to inherit Resource class and make own class. resource manager connect to the instrument and return Resource instance. so i just want to use returned Resource object like my own class – usan Jun 01 '21 at 03:49
  • ```session = visa.ResourceManager.open_resource(visa_resource_name)``` in this situation. i want to use session like my own class object – usan Jun 01 '21 at 03:59

1 Answers1

0

You can do this by setting __class__ of b to B. But read this first: https://stackoverflow.com/a/13280789/6759844

class A:
    def __init__(self):
        pass # some member variables are assigned
    pass

class B(A):
    def test(self):
        print("test")
    pass

b = A()
b.__class__ = B
b.test() # error
Brian Destura
  • 11,487
  • 3
  • 18
  • 34