471,075 Members | 850 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,075 software developers and data experts.

Functions, parameters

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
Feb 8 '07 #1
7 1446
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...
Feb 8 '07 #2
>>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
Feb 8 '07 #3
Boris Ozegovic <si****************@com.gmailwrites:
>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
Feb 8 '07 #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
Feb 8 '07 #5
Boris Ozegovic <si****************@com.gmailwrites:
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'.
Feb 8 '07 #6
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
Feb 8 '07 #7
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...
Feb 8 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by BlueDragon | last post: by
5 posts views Thread by Zlatko Matiæ | last post: by
9 posts views Thread by Gibby Koldenhof | last post: by
2 posts views Thread by anoop | last post: by
13 posts views Thread by Simon Dean | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.