(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
Related
I have trouble with understanding what happens when overriding a function that is used within another function in python class inheritance. Now say I have two classes A and B, with B as subclass, with the folliwing definitions:
class A:
def __init__(self):
self.var = 1
def print(self):
self._print()
def _print(self):
print(self.var)
class B(A):
def __init__(self):
self.var = 1
def _print(self):
print(self.var * 2)
now say:
aa = A()
bb = B()
Now, in the above code, _print() is overridden in B. As I expect, calling bb.print() prints 2, as does calling bb.print(). However, executing super(B, bb).print() also prints 2, and only executing super(B, bb)._print() prints 1. This is what I do not fully understand. I expected to have super(B, bb).print() printing 1 as I call a method of the superclass.
Could someone elaborate on this behaviour?
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.
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"
class A():
class B():
def Foo(self):
print "Hello"
class C():
def Bar(self):
print "Goodbye"
def name(self):
print "FooBar"
What I want to do is, within the Bar function is call the Foo function. How would I do that?
In Python, inner classes don't have an implicit instance of the outer class associated with them. Without such an instance, you can't call A's non-static methods from B or C.
If you do have such an instance, then simply use the dot notation:
class C():
def Bar(self):
self.a.name()
...
(where self.a is an instance of A.)
Alternatively, if A.name() can be made static, the following will also work:
class A(object):
class C():
def Bar(self):
print "Goodbye"
A.name()
#staticmethod
def name():
print "FooBar"
A.C().Bar()
You need an instance of class A to call a method.
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. :)