I am just playing around with the language, but wonder if it is possible to use functions defined within a scope of the class without explicitly defining self as the first argument.
I understand the "proper" way to implement a class might be
class minimal:
variable = 1
def add(self,x,y):
return x+y
def __init__(self,x):
self.value = self.add(x,self.variable)
m = minimal(1)
print m.value
--> 2
However, if I define and apply add in a similar way as variable (in the scope of the class), then I get an error (expected):
class minimal:
variable = 1
def add(x,y):
return x+y
def __init__(self,x):
self.value = self.add(x,self.variable)
m = minimal(1)
print m.value
--> TypeError: add() takes exactly 2 arguments (3 given)
Is there a way around this? Or this is it generally advised that everything be defined with explicit reference to self (i.e. self.variable=1 defined in the __init__ method and add defined with self as first argument)?
Edit __init__ method corrected to assign to self.value in the second case instead of trying to return a value (unintentional).