Passing data attributes as method parameters

P

Panos Laganakos

Hello,

I'd like to know how its possible to pass a data attribute as a method
parameter.

Something in the form of:

class MyClass:
def __init__(self):
self.a = 10
self.b = '20'

def my_method(self, param1=self.a, param2=self.b):
pass

Seems to produce a NameError of 'self' not being defined.
 
B

Ben Cartwright

Panos said:
I'd like to know how its possible to pass a data attribute as a method
parameter.

Something in the form of:

class MyClass:
def __init__(self):
self.a = 10
self.b = '20'

def my_method(self, param1=self.a, param2=self.b):
pass

Seems to produce a NameError of 'self' not being defined.

Default arguments are statically bound, so you'll need to do something
like this:

class MyClass:
def __init__(self):
self.a = 10
self.b = '20'

def my_method(self, param1=None, param2=None):
if param1 is None:
param1 = self.a
if param2 is None:
param2 = self.b

--Ben
 
P

Panos Laganakos

Thanks Ben.

What does it mean that they're statically bound?

It seems weird that I'm not able to access variables in the class
namespace even though these attributes come into existance after class
instantiation.
 
J

Jay Parlar

Thanks Ben.

What does it mean that they're statically bound?

It seems weird that I'm not able to access variables in the class
namespace even though these attributes come into existance after class
instantiation.

The parameters are "put together" and bound to the method when the
class is defined, *not* after class instantiation.

As an example:
.... def my_method(self, param1 = []):
.... print param1
.... param1.append(5)
....
>>> x = MyClass()
>>> x.my_method() []
>>> x.my_method() [5]
>>> y = MyClass()
>>> y.my_method() [5, 5]
>>> y.my_method() [5, 5, 5]
>>>


Usually, people use immutable datatypes as default parameter values, so
it doesn't cause a problem.

And an even more illustrative example:
.... x = 2
.... def my_method(self, param = M.x):
.... pass
....
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 3, in M
NameError: name 'M' is not defined


The class is still being built when the method is created, so even the
name "M" doesn't exist yet.




Ben's solution is probably the best way to do what you're looking for.

Jay P.
 
P

Piet van Oostrum

Panos Laganakos said:
PL> Thanks Ben.
PL> What does it mean that they're statically bound?

It means that the default values are evaluated at definition time. At that
time there isn't a variable 'self' defined. It would only work if the
defaults would be evaluated at the time the method is called, but that's
not how Python works.
PL> It seems weird that I'm not able to access variables in the class
PL> namespace even though these attributes come into existance after class
PL> instantiation.

What do you mean 'variables in the class namespace'? Which variable is in
the class namespace? Please note that you can access variables in the class
namespace:

class MyClass:

a = 10
b = 20

def my_method(self, param1=a, param2=b):
print param1, param2
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top