FAQ
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')

Search Discussions

  • John McMonagle at May 31, 2006 at 11:57 pm

    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.
  • John Machin at Jun 1, 2006 at 12:21 am

    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 ===
  • 3rdshiftcoder at Jun 1, 2006 at 2:21 am
    "John Machin" <sjmachin at lexicon.net> wrote in message
    news:447e32f3 at 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
  • 3rdshiftcoder at Jun 1, 2006 at 12:34 am
    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" <jmcmonagle at velseis.com.au> wrote in message
    news:mailman.6388.1149119924.27775.python-list at python.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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMay 31, '06 at 11:24p
activeJun 1, '06 at 2:21a
posts5
users3
websitepython.org

People

Translate

site design / logo © 2023 Grokbase