Python inherit the property of one class to another - python

I have three different class's and some variables in it
class A(object):
a = "Hello"
b = "World"
class B(object):
a = "Hello"
b = "World"
class C(object):
a = "Hello"
b = "World"
Where a = "Hello" and b = "World" is common to all class's, how can I declare these variables as a global class and Inherit the properties to these tables.
I Tried in this way, but am not able to get the solution.
class G(object):
a = "Hello"
b = "World"
class A(object, G):
pass
Here I'm trying to inherit the whole property of the class. Please help me to solve this thanks in advance.

class A(object):
a = "Hello"
b = "World"
class B(A):
something
class C(B):
something
c = C()
print(c.a,c.b)
You don't have to re-declare a and b each time or else why would you inherit in the first place. If you declare them you basically are overwriting the parent's variable with the child's. If you do that you can call the parent's one with super().

This is how you do it. Please let know in comments if there is something that you don't understand, or if I am doing anything wrong.
class A(object):
a = "Hello"
b = "World"
class B(A):
pass
class C(B):
pass
c = C()
print(c.a,c.b)
Prints Hello World

Define class A as:
class A(G):
pass
The problem with repeating the base class object is that it creates a base class resolution conflict, since A was inheriting it twice: Once directly, and once indirectly through class G.

Related

Can a specific base class object be set as the base object of an inherited class/object in python?

I have a class A and a class B derived from A.
After creating an instance of class A with many operations performed, I now want to serialize that specific object. Let's call that object A_instance.
When initializing class B, how can I tell B that it's base object should be A_instance?
Within B's init i want to decide whether it should normally execute super().__init__(...) or setting the base object directly to A_instance.
Here is a code example which makes my question hopefully clear:
class A():
def __init__(self, a=1):
self.a = a
self.message = "Hello, I'm class A"
myA = A(15)
class B(A):
def __init__(self, b=2, my_base=None):
if my_base:
# what should i code here? maybe someting like super().super_object = my_base
pass
else:
super(B, self).__init__()
self.b = b
self.message = "Hello, I'm class B inherited from A"
#Then the above code should result in something like:
myB = B(my_base=myA)
assert myB.a == myA.a
A similar if not even the same question for C++ can be found here:
set the base object of derived object?

Python class order

I was just trying some things with the python classes and wondered about the order of them. Does python offer a solution to put the class C before class A and B and still inherit things from them?
class A:
def input_name(self):
user_input_name = input("Name please\n")
A.user_input_name = user_input_name
class B:
def input_age(self):
user_input_age = input("Age please\n")
B.user_input_age = user_input_age
class C(A,B):
def output(self):
print(C.user_input_name)
print(C.user_input_age)
print("Hi {0}!\n".format(self.user_input_name))
eka = A()
eka.input_name()
toka = B()
toka.input_age()
kolmas = C()
kolmas.output()
No, you can't. class is an executable statement that creates the class object at runtime and bind it to the class name in the current scope. Until this statement is executed, the class object doesn't exist and the name is not defined either, so you cannot reference it.

python can we override a method by inheriting 2 class

class A(object):
def print_some(self):
print 'a'
class B(object):
def print_some(self):
print 'b'
class C(A, B):
pass
c = C()
print c.print_some()
'a'
What i expect of the output is 'b'. The reason i want to do this is because i want to override some method, let's say form_valid from CreateView in django, simply by inheriting a class i write containing custom form_valid, or there are better approaches?
class A is first (left) in the class C(A, B) instruction, so you are getting the print_some method from it (A class). Read here.
From your question I expect you can change the inheritance of B and C, can't you? So why don't you build up the inheritance like
A <- B <- C
Or in code:
class A(object):
....
class B(A):
...
class C(B):
...
This should give you the desired output.

using variables in class functions in another class (python)

I want to use the variables i have declared inside a function in one class, in another class.
For example i want to use the variable "j" in another class. Is it possible? (I read somewhere that it might have something to do with instance variables but fully couldn't understand the concept).
class check1:
def helloworld(self):
j = 5
class check1:
def helloworld(self):
self.j = 5
check_instance=check1()
print (hasattr(check_instance,'j')) #False -- j hasn't been set on check_instance yet
check_instance.helloworld() #add j attribute to check_instance
print(check_instance.j) #prints 5
but you don't need a method to assign a new attribute to a class instance...
check_instance.k=6 #this works just fine.
Now you can use check_instance.j (or check_instance.k) just like you would use any other variable.
This may seems a little bit like magic until you learn that:
check_instance.helloworld()
is completely equivalent to:
check1.helloworld(check_instance)
(If you think about it a little bit, that explains what the self parameter is).
I'm not completely sure what you're trying to achieve here -- There are also class variables which are shared by all instances of the class...
class Foo(object):
#define foolist at the class level
#(not at the instance level as self.foolist would be defined in a method)
foolist=[]
A=Foo()
B=Foo()
A.foolist.append("bar")
print (B.foolist) # ["bar"]
print (A.foolist is B.foolist) #True -- A and B are sharing the same foolist variable.
j cannot be seen by another class; however, I think you meant self.j, which can.
class A(object):
def __init__(self, x):
self.x = x
class B(object):
def __init__(self):
self.sum = 0
def addA(self, a):
self.sum += a.x
a = A(4)
b = B()
b.addA(a) # b.sum = 4
Using class inheritane it is very easy to "share" instance variables
example:
class A:
def __init__(self):
self.a = 10
def retb(self):
return self.b
class B(A):
def __init__(self):
A.__init__(self)
self.b = self.a
o = B()
print o.a
print o.b
print o.retb()

Inheriting properties of a separate class in Python

I am instantiating a class inside another one:
class A(F):
def __init__(self):
return
b = B()
Class B also inherits class F:
class B(F):
def __init__(self):
return
There are properties of F which have been defined in class A, which I need to access inside class B. (a MySQL connection and a logging handler.)
I would like B to have the properties which have been set to F, when they were instantiated initially in A, so I can use the logging/mysql handlers inside B without re-instantiating them.
How can I go about this? Sorry if the question is unclear.
Put the stuff you want to share in F and both A and B will be able to share it. Eg
class F(object):
def useful(self):
pass
class A(F):
def something(self):
self.useful()
class B(F):
def something_else(self):
self.useful()

Categories