Very simple question here, but I am new to python as an object oriented programming language. I am trying to write a class. Imagine it is organized as follows:
class myClass:
def __init__(self,a,b,runit=True):
self.a = a
self.b = b
if runit:
self.run_func()
def run_func(self):
self.c = self.a*self.b
return
So as you can see the class is initialized with just a and b. It defaults to initialize c from those arguments, but it need not. Now let me illustrate three use cases that I think should behave the same, but are not:
# Use 1
test = myClass(5,2)
print(test.c)
# Use 2
test = myClass(5,2,runit=False)
test.run_func()
print(test.c)
# Use 3
test = myClass(5,2,runit=False).run_func()
print(test.c)
This returns the following:
10
10
Traceback (most recent call last):
File "<ipython-input-37-cb854baa3a0c>", line 23, in <module>
print(test.c)
AttributeError: 'NoneType' object has no attribute 'c'
Why can the instantiated class not be operated on immediately and pipe this result to test in one step? In my head, (1) and (2) are the same set of operations except one is broken into two steps and the other is done in one line.
And, more importantly, how can I fix this by editing my class to behave in the expected manner?