parent.py
:
class A(object): def methodA(self): print("in methodA")
child.py
:
from parent import A class B(A): def methodb(self): print("am in methodb")
Is there anyway to call methodb()
in parent.py
?
You just have to create an object of the child class and call the function of the parent class using dot(.) operator.
Accessing Parent Class Functions This is really simple, you just have to call the constructor of parent class inside the constructor of child class and then the object of a child class can access the methods and attributes of the parent class.
Yes it seems that if you override the super/base-classes's functions, calls to those functions in the base class will go to the child/derived class.
To have a derived function call a base function of the same name, simply do a normal function call, but prefix the function with the scope qualifier (the name of the base class and two colons). The following example redefines Derived::identify() so it first calls Base::identify() and then does its own additional stuff.
Doing this would only make sense if A
is an abstract base class, meaning that A
is only meant to be used as a base for other classes, not instantiated directly. If that were the case, you would define methodB
on class A, but leave it unimplemented:
class A(object): def methodA(self): print("in methodA") def methodB(self): raise NotImplementedError("Must override methodB") from parent import A class B(A): def methodB(self): print("am in methodB")
This isn't strictly necessary. If you don't declare methodB
anywhere in A
, and instantiate B
, you'd still be able to call methodB
from the body of methodA
, but it's a bad practice; it's not clear where methodA
is supposed to come from, or that child classes need to override it.
If you want to be more formal, you can use the Python abc
module to declare A as an abstract base class.
from abc import ABC, abstractmethod class A(ABC): def methodA(self): print("in methodA") @abstractmethod def methodB(self): raise NotImplementedError("Must override methodB")
Or if using Python 2.x:
from abc import ABCMeta, abstractmethod class A(object): __metaclass__ = ABCMeta def methodA(self): print("in methodA") @abstractmethod def methodB(self): raise NotImplementedError("Must override methodB")
Using this will actually prevent you from instantiating A
or any class that inherits from A
without overriding methodB
. For example, if B looked like this:
class B(A): pass
You'd get an error trying to instantiate it:
Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: Can't instantiate abstract class B with abstract methods methodB
The same would happen if you tried instantiating A
.
You can do something like this:
class A(): def foo(self): self.testb() class B(A): def testb(self): print('lol, it works') b = B() b.foo()
Which would return this of course:
lol, it works
Note, that in fact there is no call from parent, there is just call of function foo
from instance of child class, this instance has inherited foo
from parent, i.e. this is impossible:
a=A() a.foo()
will produce: AttributeError: A instance has no attribute 'testb'
because
>>> dir(A) ['__doc__', '__module__', 'foo'] >>> dir(B) ['__doc__', '__module__', 'foo', 'testb']
What I've wanted to show that you can create instance of child class, and it will have all methods and parameters from both parent and it's own classes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With