variables of the class are not available as default values?

seanacais kccnospam at glenevin.com
Thu Aug 27 17:37:54 EDT 2009


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



More information about the Python-list mailing list