Python: automatically call a parent function after child instantiation - python

Python 2.7
I would like to automotically call a function of a parent object after I instantiate its child
class Mother:
def __init__(self):
pass
def call_me_maybe(self):
print 'hello son'
class Child(Mother):
def __init__(self):
print 'hi mom'
# desired behavior
>>> billy = Child()
hi mom
hello son
Is there a way I can do this?
Edit, from a comment below:
"i should have made it more clearer in my question, what i really want is some sort of 'automatic' calling of the parent method triggered solely by the instantiation of the child, with no explicit calling of the parent method from the child. I was hoping there would be some sort of magic method for this but i dont think there is."

You could use super but you should set your superclass to inherit from object:
class Mother(object):
# ^
def __init__(self):
pass
def call_me_maybe(self):
print 'hello son'
class Child(Mother):
def __init__(self):
print 'hi mom'
super(Child, self).call_me_maybe()
>>> billy = Child()
hi mom
hello son

Use super()?
class Child(Mother):
def __init__(self):
print 'hi mom'
super(Child, self).call_me_maybe()

Since the child class inherits the parents methods, you can simply call the method in the __init__() statement.
class Mother(object):
def __init__(self):
pass
def call_me_maybe(self):
print('hello son')
class Child(Mother):
def __init__(self):
print('hi mom')
self.call_me_maybe()

Related

How can I implement a method to print an object in the super class?

When I run this, I get the desired string corresponding to the species object that is created. My goal here is to be able to create a method called speak in the Animal class that achieves the same thing. But I don't have a good understanding on how to do that.
class Animal(object):
def __init__(self):
pass
def speak():
pass
class Mammal(Animal):
def __init__(self):
Animal.__init__(self)
class Cat(Mammal):
def __init__(self):
Mammal.__init__(self)
def __str__(self):
return "meeeow"
class Dog(Mammal):
def __init__(self):
Mammal.__init__(self)
def __str__(self):
return "wooof"
class Primate(Mammal):
def __init__(self):
Mammal.__init__(self)
class Hacker(Primate):
def __init__(self):
Primate.__init__(self)
def __str__(self):
return "Hello world!"
garfield = Cat()
print(garfield)
spike = Dog()
print(spike)
john = Hacker()
print(john)
Yours inherited classes can access any method defined in the parent class. So to achieve this, just define a new method called speak in Animal class and print as you did in your examples:
class Animal(object):
def __init__(self):
print(self)
pass
def speak(self):
print(self)
Then just create the instance and call the function:
garfield = Cat()
garfield.speak()
Output
meeeow
Modify your Animal class like below. When you call the super method from your subclass, you're passing the instance of the class you created to the parent's init function. So the instance of class is passed to the Mammal init function, which calls super and passes the instance to the Animal init function.
class Animal(object):
def __init__(self):
self.speak()
def speak(self):
print(self)

Calling Parent method directly in 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.

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")

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