default argument

B

Back9

Hi,

Is this grammer working in Python?

class test:
self._value = 10
def func(self, self._value)

When i try it, it complains about undefined self.

i don't know why.

TIA
 
B

Back9

Hi,

Is this grammer working in Python?

class test:
  self._value = 10
  def func(self, self._value)

When i try it, it complains about undefined self.

i don't know why.

TIA

Sorry
here is the what i meant
class test:
self._value = 10
def func(self, pos = self._value)
 
C

Chris Rebert

On May 11, 3:06 pm, Back9 <[email protected]> wrote:

Sorry
here is the what i meant
class test:
 self._value = 10
 def func(self, pos = self._value)

You're still defining the class, so how could there possibly be an
instance of it to refer to as "self" yet (outside of a method body)?
Also, just so you know, default argument values are only evaluated
once, at the time the function/method is defined, so `pos =
self._value` is never going to work.

Do you mean for self._value to be a class variable (Java lingo: static
variable), or an instance variable?

Cheers,
Chris
 
B

Back9

You're still defining the class, so how could there possibly be an
instance of it to refer to as "self" yet (outside of a method body)?
Also, just so you know, default argument values are only evaluated
once, at the time the function/method is defined, so `pos =
self._value` is never going to work.

Do you mean for self._value to be a class variable (Java lingo: static
variable), or an instance variable?

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

self._value will be instance variable
 
C

Chris Rebert

self._value will be instance variable

class Test(object):
def __init__(self):
self._value = 10
def func(self, pos=None):
if pos is None:
pos = self._value
#do whatever

Using None like this is the idiomatic way to have non-constant or
mutable default argument values in Python.

I recommend you read the part of the Python tutorial on
object-oriented programming:
http://docs.python.org/tutorial/classes.html

Cheers,
Chris
 
J

j vickroy

Back9 said:
Hi,

Is this grammer working in Python?

class test:
self._value = 10
def func(self, self._value)

When i try it, it complains about undefined self.

i don't know why.

TIA

.... not exactly; try:

class Test:
_value = 10
def func(self):
print id(self._value), self._value
print id(Test._value), Test._value

t = Test()
t.func()
 
D

Dave Angel

Back9 said:
self._value will be instance variable
If you want an instance value to be your default, you'll need to us an
indirect approach. There are no instances at the time the class is
defined. So you want to create such a value in the __init__() method.
Something like the following (untested):

class Test(object):
def __init__(self, initvalue):
self.value = initvalue
def func(self, pos = None):
if pos=None: pos = self.value
etc.

x = Test(44)
x.func(91) #uses 91 for pos
x.func() #uses 44 for pos


DaveA
 
T

Terry Reedy

self._value will be instance variable

Then set it in the __init__ method. Read the tutorial and ref manual on
Python class statements, which are a bit different from what you might
be used to.
 

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
474,173
Messages
2,570,938
Members
47,482
Latest member
solbuggy09

Latest Threads

Top