I have defined a class
code.py
class test:
def __init__(self.x,self.y):
self.x = x
self.y = y
def sum(self):
z = self.x + self.y
return z
def main():
x = test(1,2)
valsum = x.sum()
I want the sum to be returned when the class is called not when the method id called?
How will I code it
Use a function for this:
Or better, just use the builtin:
If you have good reason to want this, you can always wrap this stuff up in another function:
Or you can just do:
directly. This should be used sparingly though since it is inefficient …
Finally (as pointed out in the comments, you can do this if you override
__new__, DON’T DO THIS)