Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Passing data attributes as method parameters

Reply
Thread Tools

Passing data attributes as method parameters

 
 
Panos Laganakos
Guest
Posts: n/a
 
      04-23-2006
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.

 
Reply With Quote
 
 
 
 
Ben Cartwright
Guest
Posts: n/a
 
      04-23-2006
Panos Laganakos wrote:
> 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

 
Reply With Quote
 
 
 
 
Panos Laganakos
Guest
Posts: n/a
 
      04-23-2006
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.

 
Reply With Quote
 
Jay Parlar
Guest
Posts: n/a
 
      04-24-2006

On Apr 23, 2006, at 4:59 PM, Panos Laganakos wrote:

> 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:

>>> class MyClass:

.... 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:

>>> class M:

.... 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.

 
Reply With Quote
 
Piet van Oostrum
Guest
Posts: n/a
 
      04-24-2006
>>>>> "Panos Laganakos" <(E-Mail Removed)> (PL) wrote:

>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

--
Piet van Oostrum <(E-Mail Removed)>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
data attributes override method attributes? Jayden Python 16 09-29-2012 03:20 AM
Passing parameters to an executable vs. passing them to a server Ramon F Herrera C++ 8 09-13-2009 02:48 AM
class attributes & data attributes james_027 Python 2 06-20-2007 03:12 PM
Class Member Data and Member Function Parameters - Should Parameters Be Data Members? Jason C++ 2 05-13-2006 07:11 AM
passing parameters to run() method cppaddict Java 3 07-16-2004 06:19 PM



Advertisments