473,398 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Passing data attributes as method parameters

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.

Apr 23 '06 #1
4 1703
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

Apr 23 '06 #2
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.

Apr 24 '06 #3

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.

Apr 24 '06 #4
>>>>> "Panos Laganakos" <pa*************@gmail.com> (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 <pi**@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: pi**@vanoostrum.org
Apr 24 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Hans [DiaGraphIT] | last post by:
Hi! I have problem with passing data to custom action. I don't know what wrong I'm doing. I'm trying to follow the steps in the walkthrough: "Passing Data to a Custom Action" ...
8
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
4
by: Mike Dinnis | last post by:
Hi, I've been working through a number of turorials to try to learn more about retrieving data from a SQL database. I think i've mastered techniques where i create a sql string in the page and...
4
by: MicroMoth | last post by:
Hi, I'm trying to write a update method, in which when the user clicks the update button the update method is passed 10 form fields. Then a update SQL is run to update the database. My question...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
5
nmm32
by: nmm32 | last post by:
I have a DataGrid which displays data with the aid of a procedure. I have tested the procedure inside the database and it is working fine. I have another procedure which adds another row to the...
0
by: cms3023 | last post by:
I have a DataGrid which displays data with the aid of a procedure. I have tested the procedure inside the database and it is working fine. The table inside the database has data that matches with...
2
by: luis | last post by:
I'm using ctypes to call a fortran dll from python. I have no problems passing integer and double arryas, but I have an error with str arrys. For example: ..... StringVector = c_char_p *...
11
by: Bob Yang | last post by:
Hi, I have this in C++ and I like to call it from c# to get the value but I fail. it will be good if you can give me some information. I tried it in VB.net it works but I use almost the same way as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.