Python - Subclass - 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"

Related

Type cast between custom inherited classes

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 sub function in a class in python?

(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

How can I make a class which has a public method that cannot be inherited by its sub-class?

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

How can I tell what class in the polymorphic hierarchy contains the method I'm about to call?

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. :)

Different instances of A in B (Python)

In a situation like this b1 and b2 both have the same instanse of A.
class A:
def __init__(self):
self.var=1
class B:
a=A()
def __init__(self):
pass
b1=B()
b2=B()
b1.a.var=2 #changing "var" in b1 would also change it in b2
print(b2.a.var) # prints 2
What should i do to have 2 different instances of A in B?
With B defined as it is, its attribute a belongs to the class itself, not each individual instance. You would need to do something like this:
class B:
def __init__(self):
self.a = A()
to get separate instances of A for every B.
You were using what amounts to a static variable. Try this:
class A:
def __init__(self):
self.var = 1
class B:
def __init__(self):
self.a = A()
You need to initialize it on a per-instance basis instead of at the class level like you have now:
class B:
def __init__(self):
self.a = A()
You're initialising A() as a static class variable when it is first parsed.
To have one instance of A() per instance of B() it should be in the __init__ of B()
class A:
def __init__(self):
self.var=1
class B:
def __init__(self):
self.a = A()
b1=B()
b2=B()
b1.a.var=2 # changing "var" in b1 would not change it in b2
print(b2.a.var) # now prints 1

Categories

Resources