Reputation: 998
I have defined an object that will have an argument. Then I have created 3 methods as:
do_it1
will add 10 on value of the objectdo_it2
will add 20 on value of the objectdo_it3
could accept an argument then it should add 30 plus its input argument on what's applied on.i.e. to what is on the left side.
Here's what I did
class MyClass():
def __init__(self,object_value):
self.object_value = object_value
def do_it1 (self):
return self.object_value + 10
def do_it2 (self):
return self.object_value + 20
def do_it3 (self,input_number):
self.input_number = input_number
return + 30 + self.input_number
here is the output
MyClass(5).do_it1()
returns 15 which is correct
MyClass(4).do_it2()
returns 24 which is correct
MyClass(4).do_it2().do_it3(6)
returns AttributeError: 'int' object has no attribute 'do_it3' while I was expecting 60 as 4 + 20 + 30 + 6
MyClass(1).do_it1().do_it2().do_it3(9)
returns AttributeError: 'int' object has no attribute 'do_it2' while I was expecting 1 + 10 + 20 + 30 + 9 = 70
How I could apply a method on top of method(s) in same class
Upvotes: 2
Views: 103
Reputation: 26906
You need to return self
(or a copy of it, via import copy
, copy.copy(self)
or similar) for this to work.
The accrued value has to be accessed via the object_value
property.
In code:
class MyClass():
def __init__(self,object_value):
self.object_value = object_value
def do_it1(self):
self.object_value += 10
return self
def do_it2(self):
self.object_value += 20
return self
def do_it3(self, input_number):
self.input_number = input_number
self.object_value += 30 + self.input_number
return self
MyClass(4).do_it2().do_it3(6).object_value
# 60
MyClass(1).do_it1().do_it2().do_it3(9).object_value
# 70
(method do_it3()
may or may not do what you want, your code is invalid so I just guessed, but it is not too relevant)
Upvotes: 4
Reputation: 937
For MyClass(4).do_it2().do_it3(6) the problem is that do_it2() returns an integer; to that integer you are applying the method do_it3(), but the integer class does not have any method call do_it3(). You could try:
MyClass(4).do_it3(6 + MyClass(4).do_it2())
Upvotes: 0
Reputation: 397
As these methods return int
s, you can only call int methods on their results.
Your invocation MyClass(4).do_it2().do_it3(6)
is equivalent to 24.do_it3(6)
, which does not make much sense.
Upvotes: 0
Reputation: 15
In this statement you call init which creates an object of MyClass self.object_value = 5 and then you call do_it1() of it
MyClass(5).do_it1()
In the second one you do the same thing, but you create an object with self.object_value = 4 and then you call.do_it2()
MyClass(4).do_it2()
In the last one it returns you an error because you create the object MyClass with self.object_value = 4, then you call do_it2() which returns an Int value, so you will have Int.do_it3(6) in the end, which raises an error because the int object has no method do_it(3). All of the return value of MyClass(4).do_it2() is an int and then you call a method that is not a method of the return object.
MyClass(4).do_it2().do_it3(6)
I hope this helps. Let me know if you did not understand.
Upvotes: 0