S
seanacais
I'm working on a program where I wish to define the default value of a
method as a value that was set in __init__. I get a compilation error
saying that self is undefined.
As always a code snippet helps
class foo:
def __init__(self, maxvalue):
self.maxvalue = maxvalue
self.value = 0
def put(self, value=self.maxvalue):
self.value = value
So if I call foo.put() the value is set to maxvalue but maxvalue can
be specified when I instantiate foo.
Explanations and/or workarounds much appreciated.
python test.py
Traceback (most recent call last):
File "test.py", line 1, in <module>
class foo:
File "test.py", line 6, in foo
def put(self, value=self.maxvalue):
NameError: name 'self' is not defined
method as a value that was set in __init__. I get a compilation error
saying that self is undefined.
As always a code snippet helps
class foo:
def __init__(self, maxvalue):
self.maxvalue = maxvalue
self.value = 0
def put(self, value=self.maxvalue):
self.value = value
So if I call foo.put() the value is set to maxvalue but maxvalue can
be specified when I instantiate foo.
Explanations and/or workarounds much appreciated.
python test.py
Traceback (most recent call last):
File "test.py", line 1, in <module>
class foo:
File "test.py", line 6, in foo
def put(self, value=self.maxvalue):
NameError: name 'self' is not defined