Functions, parameters

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Thu Feb 8 14:08:10 EST 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.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...



More information about the Python-list mailing list