beginner: using parameter in functions

3

3rdshiftcoder

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


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

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'
def returnStr(x,*args):

print '%s %s me mad' % (x,args)
if x == 'd':
s = Template("delete from columndef where tblid = $tblid and
colname = $colname")
else:
return 'nope' #this else is just for illustration and testing

d = dict(tblid=t.tblid.getText(), colname=t.colName.getText())

print s.substitute(d)

return s

def delqry(self):

createfldobj = getQryStr('d')
s = createfldobj.returnStr('d')
 
J

John McMonagle

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


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

so basically the function returns nope as it stands

python is sure different from other languages i have used.

thanks for any help,
jim


Try, the following:

class getQryStr:
def __init__(self,op):
print op
self.x = 'd'
def returnStr(self, *args):

print '%s %s me mad' % (self.x,args)
if self.x == 'd':
s = Template("delete from columndef where tblid = $tblid and
colname = $colname")
else:
return 'nope' #this else is just for illustration and
testing

d = dict(tblid=t.tblid.getText(), colname=t.colName.getText())

print s.substitute(d)

return s


Regards,

John
 
J

John Machin

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 ===
 
3

3rdshiftcoder

thanks very much John!

so i can have self as function parameter as well as in a method.
that allowed me to use properties to retrieve the value set in the
constructor.
i just changed the function return statement and it worked.
i was working along these lines but couldnt get it up and running as
fast as you posted.

templating sure is a great way to create dynamic query strings.

very cool so far but still lots to learn.

thanks again,
jim
 
3

3rdshiftcoder

thanks for the help.
it is really appreciated.

i am going to do some more reading in the next couple of days.


jim
 

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

No members online now.

Forum statistics

Threads
474,296
Messages
2,571,535
Members
48,281
Latest member
DaneLxa72

Latest Threads

Top