variables of the class are not available as default values?

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
 
C

Chris Rebert

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.
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
Explanations and/or workarounds much appreciated.

Workaround:

class foo:
def __init__(self, maxvalue):
self.maxvalue = maxvalue
self.value = 0

def put(self, value=None):
self.value = self.value if value is None else value

Explanation:

Default values are only evaluated once, when the class is defined,
thus "self" is not defined at that point since the class is still
being defined when the method definition is executed and thus there
can be no instances yet anyway.

Cheers,
Chris
 
A

Andre Engels

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.

Explanation: The default value is calculated at the time the function
is defined, not at the time it is called. And at that time there is no
instance for "self" to refer to.

Workaround:


class foo:
def __init__(self, maxvalue):
self.maxvalue = maxvalue
self.value = 0

def put(self, value=None):
if value is None:
self.value = self.maxvalue
else:
self.value = value
 
S

seanacais

Workaround:

class foo:
    def __init__(self, maxvalue):
        self.maxvalue = maxvalue
        self.value = 0

    def put(self, value=None):
        self.value = self.value if value is None else value

Explanation:

Default values are only evaluated once, when the class is defined,
thus "self" is not defined at that point since the class is still
being defined when the method definition is executed and thus there
can be no instances yet anyway.

Cheers,
Chris
--http://blog.rebertia.com

That clears that up for me. Thank you!
 
B

Bruno Desthuilliers

Chris Rebert a écrit :
(snip)
Default values are only evaluated once, when the class is defined,

<clarification target="newcomers">

s/class/function/

The function objects (defined in a class statement body...) are created
before the class object itself.
 
A

Aahz

class foo:
def __init__(self, maxvalue):
self.maxvalue =3D maxvalue
self.value =3D 0

def put(self, value=3DNone):
self.value =3D self.value if value is None else value

Stylistic nit:

I'd make that

self.value = value if value is not None else self.value

The reason is that the "primary" value should go first.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top