I have two class and methods having same name .I have the object of derived class. When i call the method (foo) from derived class object it should call the base class method.
class A:
def foo(self):
print "A Foo"
class B(A):
def foo(self):
print "B Foo"
b = B()
b.foo() # "B Foo"
After doing some search i got some solution as below and not sure whether it is proper way of doing it or not
a = A()
b.__class__.__bases__[0].foo(a) # A Foo
Is there any better way of doing it.
If you're using Python 3, use super:
class A:
def talk(self):
print('Hi from A-land!')
class B(A):
def talk(self):
print('Hello from B-land!')
def pass_message(self):
super().talk()
b = B()
b.talk()
b.pass_message()
Output:
Hello from B-land!
Hi from A-land!
You can do the same thing in Python 2 if you inherit from object and specify the parameters of super:
class B(A):
def talk(self):
print('Hello from B-land!')
def pass_message(self):
super(B, self).talk()
b = B()
b.talk()
b.pass_message()
Output:
Hello from B-land!
Hi from A-land!
You can also call the method as if it were a free function:
A.talk(b)
B.talk(b) # the same as b.talk()
Output:
Hi from A-land!
Hello from B-land!
When you call the method (foo) from derived class object, it won't call the base class method, because you're overriding it. You can use another method name for your base class or derived class to solve the interference.
Related
I wonder if there is a way in Python to access the class which the object which the method belongs to is being called from. For example:
class A:
def __init__(self):
self.b = B()
def foo(self):
print('A')
class B:
def bar(self):
<something here>.foo()
a = A()
a.b.bar()
Basically I would like B's method bar to invoke A's method foo. And if b was an attribute of some other class C, to invoke C's version of foo instead.
You could add a reference to the class which instantiates B:
class A:
def __init__(self):
# pass self while instantiating B
self.b = B(self)
def foo(self):
print('A')
class B:
def __init__(self, rel_obj):
self.rel_obj = rel_obj
def bar(self):
self.rel_obj.foo() # access foo() using self.rel_obj
Similarly, you could pass an object of class C to invoke C's version of foo method.
I have the following two classes:
class A(object):
def caller(self,name):
# want to invoke call() here when name="call"
class B(A):
def call(self):
print("hello")
Given the following:
x= B()
x.caller("call") # I want to have caller() invoke call() on the name.
I don't want to check the value of name I want it to automatically invoke the the given string as a function on self.
Use __getattribute__
class A(object):
def caller(self,name):
self.__getattribute__(name)()
class B(A):
def call(self):
print("hello")
x= B()
x.caller("call")
Output
hello
can also use eval
class A(object):
def caller(self,name):
eval('self.%s()' % name)
class B(A):
def call(self):
print("hello")
x= B()
x.caller("call")
output
hello
[Finished in 0.6s]
What is wrong with the following code?
class A:
def A_M(self): pass
class B:
#staticmethod
def C(): super(B).A_M()
error (Python 2.7.3):
>>> a = A()
>>> a.B.C()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "..x.py", line 36, in C
def C(): super(B).A_M()
NameError: global name 'B' is not defined
Edit:
the solution was simple as this:
class A:
def A_M(self): pass
class B:
#staticmethod
def C(): A().A_M() #use of A() instead of supper, etc.
Important Note that there is an issue with this solution. If you change the name of super class (i.e. A) then you will have to update all uses inside itself as A :)).
class A(object):
def foo(self):
print('foo')
#staticmethod
def bar():
print('bar')
class B(object):
#staticmethod
def bar(obj):
# A.foo is not staticmethod, you can't use A.foo(),
# you need an instance.
# You also can't use super here to get A,
# because B is not subclass of A.
obj.foo()
A.foo(obj) # the same as obj.foo()
# A.bar is static, you can use it without an object.
A.bar()
class B(A):
def foo(self):
# Again, B.foo shouldn't be a staticmethod, because A.foo isn't.
super(B, self).foo()
#staticmethod
def bar():
# You have to use super(type, type) if you don't have an instance.
super(B, B).bar()
a, b = A(), B()
a.B.bar(a)
b.foo()
B.bar()
See this for details on super(B, B).
You need to use a fully-qualified name. Also, in python 2.7, you need to use (object), else super(A.B) will give TypeError: must be type, not classobj
class A(object):
def A_M(self):
pass
class B(object):
#staticmethod
def C():
super(A.B).A_M()
Finally, super(A.B) is essentially object here. Did you mean for B to inherit from A? Or were you simply looking for A.A_M()?
A latecommer, to just encapsulate B in A the easy way is this:
class A:
def A_M(self):
return "hi"
class B:
#staticmethod
def C():
return A().A_M()
a = A()
print a.B().C()
Not sure this is what you need, but the question was still unsolved, so I guessed.
I have a utility class from which I want to use one of the member function in another class. I don't want to inherit from that class. I just want to re-use the code from one of the member function of the other class. Kind of partial inheritance.
class HugeClass():
def interestedFunc(self,arg1):
doSomething(self.someMember1)
def OtherFunctions(self):
...
class MyClass():
def __init__(self):
self.someMember1 = "myValue"
self.interestedFunc = MagicFunc(HugeClass.interestedFunc)
c = MyClass()
print c.interestedFunc(arg)
Is there such a MagicFunc in python?
You can do what you want ie.:
class Foo(object):
def foo(self):
print self.a
class Bar(object):
foo = Foo.__dict__['foo']
b = Bar()
b.a = 1
b.foo()
But are you sure that this is good idea?
It seems like the __dict__ part from the older answer is not required in Python 3
This works fine:
class Foo:
def foo(self):
print self.a
class Bar:
foo = Foo.foo
b = Bar()
b.a = 1
b.foo()
I was reading the Python docs about classes and came across this paragraph which I'm not sure about:
Derived classes may override methods
of their base classes. Because methods
have no special privileges when
calling other methods of the same
object, a method of a base class that
calls another method defined in the
same base class may end up calling a
method of a derived class that
overrides it. (For C++ programmers:
all methods in Python are effectively
virtual.)
Example:
class A:
def foo(self):
self.bar()
def bar(self):
print "from A"
class B(A):
def foo(self):
self.bar()
def bar(self):
print "from B"
Does this mean that an object of class A obj = A() can somehow end up printing "from B"? Am I reading this correctly? I apologize if this doesn't make sense. I'm a bit confused as to how python handles Inheritance and overriding. Thanks!
No. There's no way the superclass can know anything about the subclass. What it means is if you instantiate the subclass B, and it inherits a method foo(), and overrides a method bar(), then when you call foo(), that will call the bar() definition in B, not the bar() definition in A. This is not what the superclass writer intended - he expected his call to bar() to go to his own definition.
My answer doesn't necessarily contradict the ones posted already, but it does show a way to get the base class to print "from B" by calling the base class method from the inherited class. The base class still calls the inherited class method as it is working from the inherited self. Perhaps this is the type of situation the paragraph is referring to?
class A:
def foo(self):
self.bar()
def bar(self):
print("from A")
class B(A):
def foo(self):
super().foo()
def bar(self):
print("from B")
A().foo() #prints "from A"
B().foo() #prints "from B" but indirectly through the base class
a = A()
a.foo()
b = B()
b.foo()
a.bar = b.bar
a.foo()
output:
from A
from B
from B
No, it means that you if you have following object:
class B(A):
def bar(self):
print "from B"
and you do
obj = B()
obj.foo()
then this will print from B as foo(), which is defined in the base class, calls bar(), which is also defined in the base class, but overridden in the derived class.
At least this is how I read it.
class A:
def f(self):
print 'a.f'
self.g()
def g(self):
print 'a.g'
class B(A):
def g(self):
print 'b.g'
b = B()
b.f()
# a.f
# b.g
No, any object that is an A will invoke A.bar and print "from A"
Which overridden method is called depends on what the object is, not what other classes may be derived from its class. Think of the class as a cookie cutter, and the object as the cookie.
Not exactly:
class A:
def foo(self):
self.bar()
def foo2(self):
self.bar2()
def bar(self):
print "Bar A"
def bar2(self):
print "Bar2 A"
class B(A):
def bar(self):
print "Bar B"
objA = A()
objA.foo()
objA.foo2()
objB = B()
objB.foo()
objB.foo2()
Output:
Bar A
Bar2 A
Bar B
Bar2 A