Can i divide my class(parent)addition answer in class(child)? - python

class a:
def __init__(self,x):
self.x=x
def __str__(self):
return 'addition is(%d)'%(self.x)
def __add__(self,other):
c=self.x+other.x
return a(self.x+other.x)
a1=a(2)`enter code here`
a2=a(5)
c=a1+a2
print(c)
class b(a):
can i divide my parent class addition answer(c)=7 in class b and how ?

I am still not really sure what you are looking for, but it sounds like you want a child class of a named b, which can do division. Here is what that would look like:
class a:
def __init__(self,x):
self.x=x
def __str__(self):
return string(self.x)
def __add__(self,other):
c=self.x+other.x
return a(self.x+other.x)
class b(a):
def __init__(self,x):
a.__init__(self, x)
def __div__(self, other):
return self.x/other.x
a1 = a(2)
a2 = a(5)
a3 = a1+a2
print(a3)
#7
a4 = b(21)
print(a4/a3)
#3
You need a call to initiate the super class to use it in a subclass, which is what
def __init__(self,x):
a.__init__(self, x)
does. Once you have that, you can access all the superclass functions and fields, along with functions and fields defined for the subclass.

Related

Can you make a class attribute a specific instance of that class within the class definition?

Was wondering if there was a way to set a class attribute to a specific instance from within the class definition. For example,
class Value:
def __init__(self, x):
self.x = x
# Something like
# half = Value(0.5)
>>> Value.half.x
0.5
>>> Value.half.half.x
0.5
I'm also aware I can easily set it outside the class that seems a bit more bulky and error prone, like this
class Value:
def __init__(self, x):
self.x = x
Value.half = Value(0.5)
>>> Value.half.x
0.5
>>> Value.half.half.x
0.5
No. At the time the body of the class is being evaluated, the class doesn't yet exist. A class statement is a declarative syntax for calling a metaclass:
class Value:
def __init__(self, x):
self.x = x
is roughly equivalent to
def init(self, x):
self.x = x
Value = type('Value', (object,), {'__init__': init})
Your class attribute would have to be a member of the dict passed as the third argument, which has to be fully defined before type is called.
not quite, but you can make a class method that return a new instance of your class in whatever way you want with the classmethod decorator
>>> class Value:
def __init__(self, x):
self.x=x
def __repr__(self):
return f"{type(self).__name__}({self.x})"
#classmethod
def half(cls):
return cls(0.5)
>>> Value(10)
Value(10)
>>> Value.half()
Value(0.5)
>>>
look like in py3.9 you can combine it with the property decorator to accomplish just that, see linked documentation above (but I don't have it at the moment)
Simply, you can't because the class hasn't yet existed. But you can use either metaclass or class decorator to achieve the same goal as the following shows:
#Metaclass
class Meta(type):
def __init__(cls, clsname, clsbases, clsdict):
cls.half = cls(0.5)
class Value(metaclass=Meta):
def __init__(self, x):
self.x = x
#Decorator
def decorator(cls):
cls.half = cls(0.5)
return cls
#decorator
class Value2:
def __init__(self, x):
self.x = x
print(Value.half.half.x)
print(Value.half.x)
print(Value2.half.half.x)
print(Value2.half.x)

Can we pass the class object inside its own method?

I have a class A object method which uses another class B object's method, which the argument is class A object.
class A():
def calculate(self):
B = B.calculator(A)
class B():
def calculator(self, A):
...do something with A.attributes
It is possible to just pass attributes into the object, but I would see this possibility as the last priority. I am definitely a bit oversimplify my case, but I am wondering if there is a way to pass the entire class
Edit:
Sorry for the confusion. At the end I am trying to call class A object and A.calculate(), which will call class B obj and calculator function.
class A():
def __init__(self, value):
self.value = value
def calculate(self):
Bobj = B()
Bobj.calculator(A)
class B():
def calculator(self, A):
...do something with A.value
def main():
Aobj = A(value)
Aobj.calculate()
Your scenario does not currently indicate that you want to use any information from B when calculating A. There are a few ways of getting the functionality that you want.
Scenario: B stores no information and performs calculation. B should be a function
def B(value):
```do something with value```
return
class A():
def __init__(self, value):
self.value = value
def calculate(self):
return B(self.value)
def main():
Aobj = A(value)
Aobj.calculate()
Scenario: B stores some other information, but internal B information is not needed for the calculation. B should have a static method
class B():
#staticmethod
def calculate(value):
```do something with value```
return
class A():
def __init__(self, value):
self.value = value
def calculate(self):
return B.calculate(self.value)
def main():
Aobj = A(value)
Aobj.calculate()

Use methods of parent class A from parent class B

I have a class A:
class A(object):
def pprint(x):
print(x)
Then I have a class B:
class B(object):
def pprint(x):
x += 1
# find a way to call A.pprint(x)
Then I have a child class:
class Child(B, A):
pass
Which should be used:
child = Child()
child.pprint(1)
>>> 2
I can make changes to B but not to A. I cannot refer to A directly in B. B will never be instantiated directly, always via children class.
After the explanation - what you need is not super() you need something like sibling_super() to find the next class in the multiple inheritance chain. You can poll Python's MRO for that, for example:
class A(object):
def pprint(self, x): # just to make it valid, assuming it is valid in the real code
print(x)
class B(object):
#staticmethod
def sibling_super(cls, instance):
mro = instance.__class__.mro()
return mro[mro.index(cls) + 1]
def pprint(self, x):
x += 1
self.sibling_super(B, self).pprint(self, x)
class Child(B, A):
pass
child = Child()
child.pprint(1) # 2
You have a couple of options for accessing the A method from the B class without having B inherit from A.
First, you could create a staticmethod and call it from B.
class A(object):
#staticmethod
def pprint(x):
print(x)
class B(object):
def pprint(self, x):
print(x + 1)
A.pprint(x)
Or you could inherit A in B like this:
class A(object):
def pprint(self, x):
print(x)
class B(A):
def pprint(self, x):
print(x + 1)
super(B, self).pprint(x)
Then for your Child class only inherit from B:
class Child(B):
pass
>>> c = Child()
>>> c.pprint(1)
2
1
OK, newest solution.
import inspect
class C(B, A):
def pprint(self, x):
a_class = inspect.getmro(Child)[-2]
a_class.pprint(self, x)
Since object will be the last result in inspect.getmro(Child) we skip that one to get the one before the last one, which is A. We then call that class's pprint method. You could also, to be more sure, if you know the __name__ of the class you want to call, iterate over the results from inspect.getmro(Child) and find the one that you want.

Why we using super() when inherit is already enough

I saw an example that is about super();
class Class1:
def __init__(self):
pass
def name(self):
return "My name is"
class Class2(Class1):
def __init__(self):
pass
def name(self):
return super(Class2, self).name() + " Tom"
#now lets call the object.
b = Class2()
print(b.name())
#Output is 'My name is Tom'
Here is another version of this one without super() that I made on the above codes;
class Class1:
def __init__(self):
pass
def name(self):
return "My name is"
a = Class1()
class Class2(Class1):
def __init__(self):
pass
def name(self):
return a.name() + " Tom"
b = Class2()
print(b.name())
Output is same. So while we can inherit the abilites from Class1, we can use that abilities already. Then why we need super() ?
Because you're using the wrong example. Try to do this without super():
class Base:
def __init__(self, x):
self.x = x
def square(self):
return self.x**2
class Derived(Base):
def __init__(self, x, y):
super().__init__(x)
self.y = y
def square(self):
return super().square() + self.y**2
class CubeMixin(Base):
def square(self):
return self.x**3
class MultiplyDerived(Derived, CubeMixin):
pass
Edited to use multiple inheritance, per chepner in the comments.
When Derived calls super().square(), it gets CubeMixin.square(), rather than Base.square(). If it hadn't used super(), there would be no way for it to know that CubeMixin.square() even existed.
What you did here is called composition, although you missed it by a little.
For large codebase it's generally considered a better approach, because it alleviates issues with debugging. Imagine you had a class that inherited a class which inherited a class... After a while it tends to get hard to keep up. Proper example of a composition would be:
class a(object):
def __init__(self, a):
self.a = a
class b(object):
def __init__(self, b):
self.b = b
class rect(object):
def __init__(self, x, y):
self.a = a(x)
self.b = b(y)
Which seems a bit silly for something stupid, but has pretty much the same uses as inheritance. Additionally, it may take a bit more code (a decorator) to make the class instances not behave like objects so you don't bother the end user.
Get a better example, and comparison composition vs inheritance Here

Inheritance and base class method call python

I would like a method in a base class to call another method in the same class instead of the overriding method in an inherited class.
I would like the following code to print out
Class B: 6
Class A: 9
Can this be done?
# Base class definition
class ClassA(object):
def __init__(self):
print("Initializing A")
# hoping that this function is called by this class's printFnX
def fnX(self, x):
return x**2
def printFnX(self, x):
print("ClassA:",self.fnX(x))
# Inherits from ClassA above
class ClassB(ClassA):
def __init__(self):
print("initizlizing B")
def fnX(self, x):
return 2*x
def printFnX(self, x):
print("ClassB:", self.fnX(x))
ClassA.printFnX(self,x)
bx = ClassB()
bx.printFnX(3)
Congratulations, you've discovered the motivating use case for Python's double-underscore name mangling :-)
For the details and a worked-out example see: http://docs.python.org/tutorial/classes.html#private-variables and at http://docs.python.org/reference/expressions.html#atom-identifiers .
Here's how to use it for your example:
# Base class definition
class ClassA(object):
def __init__(self):
print("Initializing A")
# hoping that this function is called by this class's printFnX
def fnX(self, x):
return x**2
__fnX = fnX
def printFnX(self, x):
print("ClassA:",self.__fnX(x))
# Inherits from ClassA above
class ClassB(ClassA):
def __init__(self):
print("initizlizing B")
def fnX(self, x):
return 2*x
def printFnX(self, x):
print("ClassB:", self.fnX(x))
ClassA.printFnX(self,x)
bx = ClassB()
bx.printFnX(3)
The use case is described as a way of implementing the Open-Closed Principle in "The Art of Subclassing" found at http://www.youtube.com/watch?v=yrboy25WKGo&noredirect=1 .
The same can be achieved by making fnX and printFnX both classmethods.
class ClassA(object):
def __init__(self):
print("Initializing A")
# hoping that this function is called by this class's printFnX
#classmethod
def fnX(self, x):
return x ** 2
#classmethod
def printFnX(self, x):
print("ClassA:",self.fnX(x))
class ClassB(ClassA):
def __init__(self):
print("initizlizing B")
def fnX(self, x):
return 2*x
def printFnX(self, x):
print("ClassB:", self.fnX(x))
ClassA.printFnX(x)
bx = ClassB()<br>
bx.printFnX(3)

Categories