Calling Parent method directly in Python - python

I have a testmethod in the GParent class which is inherited by Parent and Child..
How can I do that?
I tried this but its not working...
GParent.testmethod(self)
class GParent():
def testmethod(self):
print "This is test method"
class Parent():
def testmethod(self):
print "This is test method"
class Child(Parent):
def __init__(self):
print "This is init method"
GParent.testmethod(self)
c = Child()

first of all: https://docs.python.org/2/tutorial/classes.html#inheritance
At any rate...
GParent.testmethod(self) <-- calling a method before it is defined
class GParent(): <-- always inherit object on your base class to ensure you are using new style classes
def testmethod(self):
print "This is test method"
class Parent(): <-- not inheriting anything
def testmethod(self): <-- if you were inheriting GParent you would be overriding the method that is defined in GParent here.
print "This is test method"
class Child(Parent):
def __init__(self):
print "This is init method"
GParent.testmethod(self) <-- if you want to call the method you are inheriting you would use self.testmethod()
c = Child()
Take a look at this code and run it, maybe it will help you out.
from __future__ import print_function #so we can use python 3 print function
class GParent(object):
def gparent_testmethod(self):
print("Grandparent test method ")
class Parent(GParent):
def parent_testmethod(self): #
print("Parent test method")
class Child(Parent):
def child_testmethod(self):
print("This is the child test method")
c = Child()
c.gparent_testmethod()
c.parent_testmethod()
c.child_testmethod()

You cannot call GParent's testmethod without an instance of GParent as its first argument.
Inheritance
class GParent(object):
def testmethod(self):
print "I'm a grandpa"
class Parent(GParent):
# implicitly inherit __init__()
# inherit and override testmethod()
def testmethod(self):
print "I'm a papa"
class Child(Parent):
def __init__(self):
super(Child, self).__init__()
# You can only call testmethod with an instance of Child
# though technically it is calling the parent's up the chain
self.testmethod()
# inherit parent's testmethod implicitly
c = Child() # print "I'm a papa"
However, two ways of calling a parent's method explicitly is through composition or class method
Composition
class Parent(object):
def testmethod(self):
print "I'm a papa"
class Child(object):
def __init__(self):
self.parent = Parent()
# call own's testmethod
self.testmethod()
# call parent's method
self.parentmethod()
def parentmethod(self):
self.parent.testmethod()
def testmethod(self):
print "I'm a son"
c = Child()
Class method
class Parent(object):
#classmethod
def testmethod(cls):
print "I'm a papa"
class Child(object):
def __init__(self):
# call own's testmethod
self.testmethod()
# call parent's method
Parent.testmethod()
def testmethod(self):
print "I'm a son"
c = Child()
It has become advisory to use composition when dealing with multiple inheritance, since inheritance creates dependency to the parent class.

Related

how to extract the class that super().__init__ comes from?

Imagine a class MyMixInClass that is being used in a multiple inheritance hierarchy. when using super() to call some method, is there some way to inspect or drill in to extract the class that this method came from?
example:
class MyMixInClass:
def __init__(self):
initfunc = getattr(super(), '__init__')
# can we figure out which class the __init__ came from?
For each class in the mro sequence, you can check if there is an __init__ method in the class __dict__:
class A:
def __init__(self):
pass
class B(A):
def __init__(self):
super().__init__()
class C(A):
pass
class D(B, C):
pass
if __name__ == '__main__':
for cls in D.__mro__:
if '__init__' in cls.__dict__:
print(f'{cls.__name__} has its own init method', end='\n')
else:
print(f'{cls.__name__} has no init method', end='\n')
output:
D has no init method
B has its own init method
C has no init method
A has its own init method
object has its own init method
In this output, the first class having an __init__ method (here B), is the one called by super().__init__() in D()

How to call super method from grandchild class?

I am working with some code that has 3 levels of class inheritance. From the lowest level derived class, what is the syntax for calling a method 2 levels up the hierarchy, e.g. a super.super call? The "middle" class does not implement the method I need to call.
Well, this is one way of doing it:
class Grandparent(object):
def my_method(self):
print "Grandparent"
class Parent(Grandparent):
def my_method(self):
print "Parent"
class Child(Parent):
def my_method(self):
print "Hello Grandparent"
Grandparent.my_method(self)
Maybe not what you want, but it's the best python has unless I'm mistaken. What you're asking sounds anti-pythonic and you'd have to explain why you're doing it for us to give you the happy python way of doing things.
Another example, maybe what you want (from your comments):
class Grandparent(object):
def my_method(self):
print "Grandparent"
class Parent(Grandparent):
def some_other_method(self):
print "Parent"
class Child(Parent):
def my_method(self):
print "Hello Grandparent"
super(Child, self).my_method()
As you can see, Parent doesn't implement my_method but Child can still use super to get at the method that Parent "sees", i.e. Grandparent's my_method.
This works for me:
class Grandparent(object):
def my_method(self):
print "Grandparent"
class Parent(Grandparent):
def my_method(self):
print "Parent"
class Child(Parent):
def my_method(self):
print "Hello Grandparent"
super(Parent, self).my_method()
If you want two levels up, why not just do
class GrandParent(object):
def act(self):
print 'grandpa act'
class Parent(GrandParent):
def act(self):
print 'parent act'
class Child(Parent):
def act(self):
super(Child.__bases__[0], self).act()
print 'child act'
instance = Child()
instance.act()
# Prints out
# >>> grandpa act
# >>> child act
You can add something defensive like checking if __bases__ is empty or looping over it if your middle classes have multiple inheritance. Nesting super doesn't work because the type of super isn't the parent type.
You can do this by following ways
class Grandparent(object):
def my_method(self):
print "Grandparent"
class Parent(Grandparent):
def my_other_method(self):
print "Parent"
class Child(Parent):
def my_method(self):
print "Inside Child"
super(Child, self).my_method()
In this case Child will call base class my_method but base class my_method is not present there so it will call base class of parent class my_method in this way we can call my_method function of grandparent
Another Way
class Grandparent(object):
def my_method(self):
print "Grandparent"
class Parent(Grandparent):
def my_other_method(self):
print "Parent"
class Child(Parent):
def my_method(self):
print "Inside Child"
super(Parent, self).my_method()
In this way we are directly calling function base class my_method function of the parent class
Another way but not pythonic way
class Grandparent(object):
def my_method(self):
print "Grandparent"
class Parent(Grandparent):
def my_other_method(self):
print "Parent"
class Child(Parent):
def my_method(self):
print "Inside Child"
Grandparent.my_method()
In this way we are directly calling my_method function by specifying the class name.
Made and tested in python 3
class Vehicle:
# Initializer / Instance Attributes
def __init__(self, name, price):
self.name = name
self.price = price
# instance's methods
def description(self):
print("\nThe car {} has a price of {} eur".format(self.name, self.price))
#Object Vehicle
m3 = Vehicle("BMW M3", 40000)
m3.description()
class Camper(Vehicle):
def __init__(self,nome,prezzo,mq):
super().__init__(nome,prezzo)
self.mq=mq
# instance's methods
def description(self):
super().description()
print("It has a dimension of",format(self.mq)+" mq")
#Camper Object(A camper is also a Vehicle)
marcopolo=Camper("Mercede MarcoPolo",80000,15)
marcopolo.description()
Output:
The car BMW M3 has a price of 40000 eur
The car Mercede MarcoPolo has a price of 80000 eur
It has a dimension of 15 mq

class based view in django

I am trying to understand class based view concepts in django. Before that i should know the function call and return statement.I would like to know what it does which I mentioned below code. I know that is about to call parent class function. what should it return. Can anyone explain this concepts with example. Thanks in advance.
class Foo(Bar):
def baz(self, arg):
return super(Foo, self).baz(arg)
I am explaining this with example.
I am creating two classes which one is inheriting from other.
class Parent(object):
def implicit(self):
print "PARENT implicit()"
class Child(Parent):
pass
dad = Parent()
son = Child()
>>>dad.implicit()
>>>son.implicit()
"PARENT implicit()"
"PARENT implicit()"
This creates a class named Child but says that there's nothing new to define in it. Instead it will inherit all of its behavior from Parent.
Now next example
class Parent(object):
def override(self):
print "PARENT override()"
class Child(Parent):
def override(self):
print "CHILD override()"
dad = Parent()
son = Child()
>>>dad.override()
>>>son.override()
"PARENT override()"
"CHILD override()"
Even though Child inherits all behaviour from Parent, Child.override messages because son is an instance of Child and Child overrides that function by defining its own version
Now next example,
class Parent(object):
def altered(self):
print "PARENT altered()"
class Child(Parent):
def altered(self):
print "CHILD, BEFORE PARENT altered()"
super(Child, self).altered()
print "CHILD, AFTER PARENT altered()"
dad = Parent()
son = Child()
>>>dad.altered()
>>>son.altered()
"PARENT altered()"
"CHILD, BEFORE PARENT altered()"
"PARENT altered()"
"CHILD, AFTER PARENT altered()"
Here we can find super(Child, self).altered(), which is aware of inheritance and will get the Parent class.(Not caring overriding)
Hope this helps
Update..In python3
super().altered() can use instead of super(Child, self).altered()
For more http://learnpythonthehardway.org/book/ex44.html
class Foo(Bar):#here Bar is your super class, you inherit super class methods
def baz(self, arg):#this class method
return super(Foo, self).baz(arg)# here super is mentioned as super class of Foo .baz(arg) is you call the super user method baz(arg)
so u need to create super class like
class BAR(object):
def baz(self,arg):
c=10+20
return c
simple example for two base class
class bar(object):
def baz(self, arg):
print"this is bar class"
class bar1(object):
def baz1(self,arg):
print "this is bar1 class"
class Foo(bar,bar1):
def baz(self, arg):
super(Foo, self).baz1(arg)
super(Foo, self).baz(arg)
a=Foo()
a.baz("hai")

Pass a parent class as an argument?

Is it possible to leave a parent class unspecified until an instance is created?
e.g. something like this:
class SomeParentClass:
# something
class Child(unspecifiedParentClass):
# something
instance = Child(SomeParentClass)
This obviously does not work. But is it possible to do this somehow?
You can change the class of an instance in the class' __init__() method:
class Child(object):
def __init__(self, baseclass):
self.__class__ = type(self.__class__.__name__,
(baseclass, object),
dict(self.__class__.__dict__))
super(self.__class__, self).__init__()
print 'initializing Child instance'
# continue with Child class' initialization...
class SomeParentClass(object):
def __init__(self):
print 'initializing SomeParentClass instance'
def hello(self):
print 'in SomeParentClass.hello()'
c = Child(SomeParentClass)
c.hello()
Output:
initializing SomeParentClass instance
initializing Child instance
in SomeParentClass.hello()
Have you tried something like this?
class SomeParentClass(object):
# ...
pass
def Child(parent):
class Child(parent):
# ...
pass
return Child()
instance = Child(SomeParentClass)
In Python 2.x, also be sure to include object as the parent class's superclass, to use new-style classes.
You can dynamically change base classes at runtime. Such as:
class SomeParentClass:
# something
class Child():
# something
def change_base_clase(base_class):
return type('Child', (base_class, object), dict(Child.__dict__))()
instance = change_base_clase(SomeParentClass)
For example:
class Base_1:
def hello(self):
print('hello_1')
class Base_2:
def hello(self):
print('hello_2')
class Child:pass
def add_base(base):
return type('Child', (base, object), dict(Child.__dict__))()
# if you want change the Child class, just:
def change_base(base):
global Child
Child = type('Child', (base, object), dict(Child.__dict__))
def main():
c1 = add_base(Base_1)
c2 = add_base(Base_2)
c1.hello()
c2.hello()
main()
Result:
hello_1
hello_2
Works well in both python 2 and 3.
For more information, see the related question How to dynamically change base class of instances at runtime?

Python - Accessing parent members

I'm facing a standstill here while trying to figure out how to have member classes access data from their parent when they are part of an external module.
Basically, this works (the B class can access is parent's methods like so: A.say_hi(A) ):
class A:
def __init__(self):
print("Initializing parent object...")
self.child = self.B()
class B:
def __init__(self):
print("Initializing child...")
A.say_hi(A)
def say_hi(self):
print("A class says hi")
However, this can get pretty messy if classes start getting extra large, so I have been placing my additional classes in files and importing them inline. The problem with that is I can no longer get the member class to access its parent's members and functions if I try to use 'import B.py' when class B is defined within.
Is there any way to get the original behavior without leaving the member class inside the same file as the parent?
Actually in your example you couldn't access instance of A in your class B. And the code A.say_hi(A) does work however is wrong. This has been said in comments to your question.
Here is how you do that if you want to be able to access parent instance:
Module b:
class B(object):
def __init__(self, parent):
self.parent = parent
def say_hi(self):
print 'Child says hi to his parent %s' % (
self.parent.__class__.__name__
)
self.parent.say_hi()
Module a:
from b import B
class A(object):
def __init__(self):
self.b = B(self)
def say_hi(self):
print 'Parent says hi!'
If you pass the object (a) to the class (b), you can call it directly.
class a():
def __init__(self):
print"Made A"
def test(self):
print ">>A Test"
class b():
def __init__(self,parent):
print"Made B"
self.parent = parent
def test(self):
print ">>B Test"
self.parent.test()
a = a()
a.test()
b = b(a)
b.test()

Categories