Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Functions, parameters

Reply
Thread Tools

Functions, parameters

 
 
Boris Ozegovic
Guest
Posts: n/a
 
      02-08-2007
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
 
Reply With Quote
 
 
 
 
Matimus
Guest
Posts: n/a
 
      02-08-2007
>>> Poll.objects.filter(question__startswith='What')

That is an example of a keyword argument. You can read about it in the
Python Tutorial:
http://docs.python.org/tut/node6.htm...00000000000000

-Matt


 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      02-08-2007
Boris Ozegovic <(E-Mail Removed)> 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
 
Reply With Quote
 
Boris Ozegovic
Guest
Posts: n/a
 
      02-08-2007
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
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      02-08-2007
Boris Ozegovic <(E-Mail Removed)> 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'.
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      02-08-2007
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.htm...00000000000000

> 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...
 
Reply With Quote
 
Boris Ozegovic
Guest
Posts: n/a
 
      02-08-2007
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
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      02-08-2007
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...
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
skipping template parameters inside template parameters kito C++ 2 09-26-2010 12:04 AM
Class Member Data and Member Function Parameters - Should Parameters Be Data Members? Jason C++ 2 05-13-2006 07:11 AM
commands to configure WLAN adapter parameters =?Utf-8?B?SmFnYW4=?= Wireless Networking 0 10-05-2005 11:37 AM
does a "parameters"-parameter overwrite the "parameters"-object? Florian Loitsch Javascript 11 03-15-2005 03:33 PM
Servlet parameters different from the command line parameters? Jonck van der Kogel Java 2 05-26-2004 11:34 PM



Advertisments