Functions, parameters

B

Boris Ozegovic

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.
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).
 
P

Paul Rubin

Boris Ozegovic said:
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
 
B

Boris Ozegovic

Bruno said:
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.
 
P

Paul Rubin

Boris Ozegovic said:
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'.
 
B

Bruno Desthuilliers

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.



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...
 
B

Boris Ozegovic

Paul said:
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.
 
B

Bruno Desthuilliers

Boris Ozegovic a écrit :
Bruno Desthuilliers wrote:



Yes, I know. :)



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...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top