Calling function from string name within object - python

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]

Related

Call method of base class from derived class object python

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.

Access class which object is being called from within method belonging to object

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.

Python recursive method calls with super

I am working with a library that relies on a recursive method call:
class A(object):
def __init__(self):
self.foo = None
def f(self):
if not self.foo:
print("Hello")
self.foo = 100
self.f()
I would like to override the method f() while using the original implementation:
class B(A):
def f(self):
super(B, self).f()
print("World")
This way, I hope to get:
Hello
World
Instead, I see:
Hello
World
World
I understand this is because the original code in class A calls self.f(), which finds B.self.
Question: What is the most Pythonic way to have "super(B, self).f()" treat self as class A, call A.f() recursively, and then return to B.f() to print "World?"
Thanks.
The only way I can see this work is for A.f() to not use self.f() but to use A.f(self) instead.
A better design is for A.f() to delegate the recursive call to a separate method:
class A(object):
def __init__(self):
self.foo = None
def f(self):
self._f_recursive()
def _f_recursive(self):
if not self.foo:
print("Hello")
self.foo = 100
self._f_recursive()
If your only option lies in B, then apart from don't override f() then, is to lie about the class, temporarily. This is not Pythonic or recommended but it'll work:
class B(A):
def f(self):
try:
self.__class__, cls = A, self.__class__
A.f(self)
finally:
self.__class__ = cls
print("World")
To be clear about this: this is not thread-safe nor the proper way to deal with this.

Python: nested class with static method fails

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.

python/pygame, pass input from a class to another class

there is a way to pass a value or a variable from a class to another class without having to pass through the main function
I'm using python
well, of course you can access other objects attributes in methods of a specific object. e.g:
class A(object):
def method(self, other):
other.somevar = 5
class B(object):
pass
def main():
a = A()
b = B()
b.somevar = "Hello World"
a.method(b)
print(b.somevar) # now prints '5'

Categories