beginner: using parameter in functions

John Machin sjmachin at lexicon.net
Wed May 31 20:21:05 EDT 2006


On 1/06/2006 9:24 AM, 3rdshiftcoder wrote:
> hi-
> 
> i am having trouble using parameter values in my function and to be honest a 
> little trouble with
> member variables.  i am trying to pass in the argument 'd' representing 
> delete.
> what the code will do is if it is 'd' it will make a delete query template 
> string.
> if it is an 'i' then insert query etc.
> 
> this is the results of my attempt to print the contents of the parameter 
> values.
> <__main__.getQryStr instance at 0x01151D50> ('d',) me mad

Exactly right, first parameter is the object itself, second parameter is 
a 1-tuple of the supplied args. See more explanation below.

> 
> 
> (and on a side note if i dont include the *args i get an invalid number of 
> parameters supplied message.)
> why is it returning the value in this format ('d',) ?
> i cant get x == d
> i guess that value 'd' is stored in a tuple and i'd like to get it out of 
> there.

No, 'd' is stored as the value of the attribute you've named "x". One of 
the main points of the whole OO caper is that objects have attributes -- 
please see later remarks about the tutorial.

> 
> so basically the function returns nope as it stands
> 
> python is sure different from other languages i have used.
> 
> thanks for any help,
> jim
> 
> 
> class getQryStr:
>     def __init__(self,op):
>         print op
>         self.x = 'd'

You probably meant
self.x = op

>     def returnStr(x,*args):

Like the first (__init__) method, this should have the mandatory "self" 
argument, plus *one* other arg .. *if* you need it. It's not apparent 
why you are calling the constructor *and* the returnStr method *each* 
with 'd'.


> 
>         print '%s %s me mad' % (x,args)
>         if x == 'd':

Here x is the object that you have created. The first argument to a 
method is the object itself, and is conventionally named "self". It must 
be declared in the method itself
     def amethod(self, arg1, arg2):
but is supplied automatically when you invoke it
     anobj.amethod('foo', 42)

[snip]

Please consider working your way through the Python tutorial
     http://docs.python.org/tut/node11.html
and/or one of the free e-books e.g.
     http://www.byteofpython.info/

At the end of this post is a modified version of your script which shows 
what is going on under normal expected usage.

HTH,
John

8<=== demo script ===

C:\junk>type use_self.py
class getQryStr:

     def __init__(self, op):
         print '__init__ ... op:%r' % op
         self.x = op

     def returnStr(self, arg):
         print 'returnStr ... self.x:%r arg:%r' % (self.x, arg)
         return '=%s=%s=' % (self.x, arg)

obj = getQryStr('blah')
print '__main__ ... obj.x:%r' % obj.x
s = obj.returnStr('yadda')
print '__main__ ... s:%r' % s

8<=== output from demo script ===

C:\junk>use_self.py
__init__ ... op:'blah'
__main__ ... obj.x:'blah'
returnStr ... self.x:'blah' arg:'yadda'
__main__ ... s:'=blah=yadda='


8<=== end ===



More information about the Python-list mailing list