Functions, parameters

Discussion in 'Python' started by Boris Ozegovic, Feb 8, 2007.

  1. Hi, I'am still learning Python and while reading Django tutorial couldn't
    understand this part:

    class Poll(models.Model):
    question = models.CharField(maxlength=200)
    pub_date = models.DateTimeField('date published')


    # Django provides a rich database lookup API that's entirely driven by
    # keyword arguments.
    >>> Poll.objects.filter(question__startswith='What')


    This 'question__startswith' is the problem. What is the common idiom for
    this type od arguments, so I can Google it? I understand what this filter
    is suppose to do, but don't know how it is done (this separation of Poll
    atribute and startwith function).

    --
    http://www.nacional.hr/articles/view/23894/23
     
    Boris Ozegovic, Feb 8, 2007
    #1
    1. Advertising

  2. Boris Ozegovic

    Matimus Guest

    Matimus, Feb 8, 2007
    #2
    1. Advertising

  3. Boris Ozegovic

    Paul Rubin Guest

    Boris Ozegovic <> writes:
    > >>> Poll.objects.filter(question__startswith='What')

    >
    > This 'question__startswith' is the problem. What is the common idiom for
    > this type od arguments, so I can Google it?


    You can refer to function args in Python by name, e.g. define a function

    def print_power(x, y):
    print x ** y

    and you can pass the parameters in by position, like in most languages:

    print_power(5, 2) # prints "25"

    You can also pass them by name, saying explicitly which arg is which
    (called "keyword arguments"):

    print_power(x=5, y=2) # also prints "25"
    print_power(y=5, x=2) # prints "32"

    You can make functions that take arbitrary named parameters. The ** below
    means that arg gets bound to a dictionary containing all the keyword args:

    def func(**keyword_args):
    print 'args are:'
    for k in keyword_args:
    print k, '=>', keyword_args[k]

    func(a=2, b=5, c='whee')

    prints:

    a => 2
    b => 5
    c => whee
     
    Paul Rubin, Feb 8, 2007
    #3
  4. Bruno Desthuilliers wrote:

    > Why don't you just read the source code ? Django is free software, you
    > know !-)


    Yes, I know. :)

    > What about something like:


    > def filter(self, **kw):
    > for argname, value in kw.items():
    > fieldname, op = argname.split('__', 1)


    Yes, this is what confused me in the first place: how to separate
    arguments. If you call split, and split returns list of String, then you
    have fieldname = 'question' and startwith = 'what', and not references at
    question and startwith, or am I missing something big.

    --
    http://www.nacional.hr/articles/view/23894/23
     
    Boris Ozegovic, Feb 8, 2007
    #4
  5. Boris Ozegovic

    Paul Rubin Guest

    Boris Ozegovic <> writes:
    > > def filter(self, **kw):
    > > for argname, value in kw.items():
    > > fieldname, op = argname.split('__', 1)

    >
    > Yes, this is what confused me in the first place: how to separate
    > arguments. If you call split, and split returns list of String, then you
    > have fieldname = 'question' and startwith = 'what', and not references at
    > question and startwith, or am I missing something big.


    Oh, I understand your question now. The call was:

    Poll.objects.filter(question__startswith='What')

    'filter' receives the argument 'kw', which is a dictionary whose value will be

    { 'question__startswith' : 'What' }

    That means the "for argname, value" loop iterates just once, with
    argname = 'question__startswith'
    and
    value = 'What'

    Since split is applied to argname, it retrieves 'question' and 'startswith'.
     
    Paul Rubin, Feb 8, 2007
    #5
  6. Boris Ozegovic a écrit :
    > Hi, I'am still learning Python and while reading Django tutorial couldn't
    > understand this part:
    >
    > class Poll(models.Model):
    > question = models.CharField(maxlength=200)
    > pub_date = models.DateTimeField('date published')
    >
    >
    > # Django provides a rich database lookup API that's entirely driven by
    > # keyword arguments.
    >
    >>>>Poll.objects.filter(question__startswith='What')

    >
    >
    > This 'question__startswith' is the problem. What is the common idiom for
    > this type od arguments, so I can Google it?


    It's a named argument - in Python we usually name them keyword args.
    http://docs.python.org/tut/node6.html#SECTION006720000000000000000

    > I understand what this filter
    > is suppose to do, but don't know how it is done (this separation of Poll
    > atribute and startwith function).
    >


    Why don't you just read the source code ? Django is free software, you
    know !-)

    What about something like:

    def filter(self, **kw):
    for argname, value in kw.items():
    fieldname, op = argname.split('__', 1)
    assert fieldname in self.fields
    # build the query here
    # etc...
     
    Bruno Desthuilliers, Feb 8, 2007
    #6
  7. Paul Rubin wrote:

    > Since split is applied to argname, it retrieves 'question' and 'startswith'.


    Exactly. :) And, 'questions' and 'startswith' are two string, and not
    references at Poll.question, or more precisely, instanceOfPoll.question.

    I suppose this is what I was looking for:


    __getattribute__(...)
    x.__getattribute__('name') <==> x.name

    Tnx guys.

    --
    http://www.nacional.hr/articles/view/23894/23
     
    Boris Ozegovic, Feb 8, 2007
    #7
  8. Boris Ozegovic a écrit :
    > Bruno Desthuilliers wrote:
    >
    >
    >>Why don't you just read the source code ? Django is free software, you
    >>know !-)

    >
    > Yes, I know. :)
    >
    >>What about something like:

    >
    >
    >>def filter(self, **kw):
    >> for argname, value in kw.items():
    >> fieldname, op = argname.split('__', 1)

    >
    >
    > Yes, this is what confused me in the first place: how to separate
    > arguments. If you call split, and split returns list of String, then you
    > have fieldname = 'question'


    and op == 'startswith'

    > and startwith = 'what', and not references at
    > question and startwith, or am I missing something big.


    The reference to 'question' is quite easy to get, since question is an
    attribute of the Poll class. Usually, one uses getattr(object, name),
    but IIRC Django model classes have a 'fields' dict (or dict-like)
    storing attributes describing the DB schema.

    Getting a reference to str.startswith() would be as easy, but it's not
    even needed. Remember, all this is used to build the WHERE clause of a
    SQL query...
     
    Bruno Desthuilliers, Feb 8, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonck van der Kogel
    Replies:
    2
    Views:
    996
    Jonck van der Kogel
    May 27, 2004
  2. Xiangliang Meng
    Replies:
    1
    Views:
    1,629
    Victor Bazarov
    Jun 21, 2004
  3. Jason
    Replies:
    2
    Views:
    525
    Jonathan Mcdougall
    May 13, 2006
  4. kito
    Replies:
    2
    Views:
    425
  5. Florian Loitsch
    Replies:
    11
    Views:
    254
    Michael Winter
    Mar 15, 2005
Loading...

Share This Page