By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,969 Members | 2,360 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,969 IT Pros & Developers. It's quick & easy.

beginner: using parameter in functions

P: n/a
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')
May 31 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Wed, 2006-05-31 at 23:24 +0000, 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
(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

--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

Jun 1 '06 #2

P: n/a
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 ===
Jun 1 '06 #3

P: n/a
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
"John McMonagle" <jm********@velseis.com.au> wrote in message
news:ma***************************************@pyt hon.org...
On Wed, 2006-05-31 at 23:24 +0000, 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
(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

--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

Jun 1 '06 #4

P: n/a

"John Machin" <sj******@lexicon.net> wrote in message
news:44******@news.eftel.com...

thanks for the help.
it is really appreciated.

i am going to do some more reading in the next couple of days.
jim
Jun 1 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.