I from the program below I want the instance of B to call the method in A but I have no idea how to type cast the object in order for the object to call the method in A which has the same name in B and so the method in A runs but not the one in B.
class A:
def doSomething(self):
print("do something in A")
class B(A):
def doSomething(self):
print('do something in B')
def main():
b = B()
b.doSomething()
(A)b.doSomething() # I want b to call the function in A instead of B
main()
If all you want to do is call the super class method doSomething on b, this should suffice:
class A(object):
def doSomething(self):
print('Do something in A')
class B(A):
def doSomething(self):
print('Do something in B')
b = B()
super(B, b).doSomething()
Which prints:
Do something in A
The idea of "type casting" isn't really applicable in python.
Related
How to call a subclass, for example, class1.class2.function, just like subdomains, shop.domain.com i tried this method
class A:
class B:
def ping():
print("Hi")
a = A()
b = a.B()
but it didn't work.
i also tried
class A:
def ping():
print("Class A")
class B(A):
def pingB():
print("Class B")
but also didn't work
You didn't add the self argument-
IN FIRST CASE:
class A:
class B:
def ping(self):
print("Hi")
a = A()
b = a.B()
b.ping()
IN SECOND CASE:
class A:
def ping():
print("Class A")
class B(A):
def pingB(self):
print("Class B")
b = B()
b.pingB()
Hope so this information is useful to you!
Happy Coding!
You still need to call the function within the B class when initializing b as the subclass B. Also, functions within a class need the self parameter.
class A:
class B:
def ping(self):
print("Hi")
a = A() # Initializing variable <a> as new object to class <A>
b = a.B() # Initializing variable <b> as new object to class <B> (Subclass of a)
b.ping() # Calling function <ping> which should print the string "Hi"
(I'm very new to both python and stackoverflow.)
def A():
def B():
print("I'm B")
A.B = B
A()
A.B()
output:
"I'm B"
This works well. What I want is to put that in a class like this(doesn't work. I just tried..)
class Student:
def A(self):
def B():
print("I'm B")
self.A.B = B
I have no Idea how to make the class and how to call the sub function in the class.
Python functions are first-class objects. What is first class function in Python
So the first piece of code is perfectly valid. It just adds a property B to the function, which can later be called using A.B().
But for the second piece of code, this is invalid, as self.A returns a reference to A method of class Student
<bound method Student.A of <__main__.Student object at 0x7f5335d80828>>
self.A does not have an attribute B, so it returns an error
AttributeError: 'method' object has no attribute 'B'
Now a quick fix would be to assign it to self.B
class Student:
def A(self):
def B():
print("I'm B")
self.B = B
a = Student()
a.A()
a.B()
Although the above code works, it is a very bad way, as you will have to always call A for every object instantiated before calling B.
You don't need to reference self because the inner function B is defined there. It should be like this:
class Student:
def A(self):
def B():
print("I'm B")
B()
I never use classes, but could you do it this way?
class A:
def __call__(self): // so you can call it like a function
def B():
print("i am B")
B()
call_A = A() // make the class callable
call_A() // call it
I want to make a class A like this
class A:
#someDecorator
def f(self):
print('A.f')
def g(self):
print('A.g')
And I can use it like this
a = A()
a.f() #prints A.f
a.g() #prints A.g
But if I declare a class B as a subclass of A, attribute f of class B will become invisible
class B(A):
pass
b = B()
b.g() #prints A.g
b.f() #error: no such attribute
How can I make this #someDecorator?
One way is to check __class__ of the method, and return if it matches A.
def disable_method(func):
def func_wrapper(name):
if name.__class__ == A:
return func(name)
else:
raise AttributeError("Cannot call inherited method")
return func_wrapper
class A:
#disable_method
def f(self):
print('A.f')
def g(self):
print('A.g')
class B(A):
pass
a = A()
a.f()
#A.f
b = B()
b.f()
#AttributeError: Cannot call inherited method
Edit: Looking at this again it is possible to check whether the method's class has a superclass by __bases__. It should work as long as its being used as a decorator.
def disable_method(func):
def func_wrapper(name):
if name.__class__.__bases__ == object:
return func(name)
else:
raise AttributeError("Cannot call method from superclass")
return func_wrapper
Hi everyone I have two classes A and B and I want to grab a method from B to use in A.
My code is along the lines of:
class A(object):
__init__(self, args):
'blah'
def func2(self, args):
#method = B.func1(args)
# method = getattr(B, 'func1')
class B(object):
__init__(self):
'do stuff'
def func1(self, args):
'Do stuff here'
return
Is there a way to get func1 into A without removing the self attribute from func1?
Neither of the two method calls are working for me and I keep getting a type error
TypeError: unbound method func1 must be called with B instance as
first argument (got NoneType instance instead)
edit: found my solution
I found the solution to my question. When I passed values from B to A I needed to pass my instance for B as well. So in my init for A
class A(object):
__init__( args, B_arg):
And in class B
class B(object):
def passattributes():
c = A( args, self )
I'm going to guess that since you're trying to do what you're trying to do, the appropriate thing to do in your case is to define B.func1 as a classmethod, because you don't expect it to require an instance of class B.
#classmethod
def func1(cls, args):
'blah
You could create an instance of B in the A class:
class A(object):
def __init__(self):
'blah'
self.bInst = B()
def func2(self, args):
method = self.bInst.func1('func1')
class B(object):
def __init__(self):
'do stuff'
def func1(self, args):
print 'Do stuff here'
return
aInst = A()
aInst.func2('some arg')
Result:
Do stuff here
If I have:
class A():
def f(self):
print("running function, f from class A")
class B(A):
def __init__(self):
A.__init__(self)
def f(self):
print("running function, f from class B")
and I make an instance of class B and call f on it, we all know we'll see the message about "from class B." But is there a way for me to inspect my object and make sure my sub-class has overridden my method? Something like:
obj = B()
assert(not obj.f.livesIn(A))
class A():
def f(self):
print("running function, f from class A")
class B(A):
def f(self):
print("running function, f from class B")
class C(A):
pass
This shows that B.f does not equal A.f. So B must override f:
obj = B()
print(obj.__class__.f == A.f)
# False
This shows that C.f equals A.f. So C must not have overridden f:
obj = C()
print(obj.__class__.f == A.f)
# True
If you want to force the child class to override, you can raise NotImplementedError().
Doing the inspection is possible too... And I see unutbu just posted an example, so I won't repeat it. :)