On Mon, 23 Jul 2007 16:57:42 -0700, to_see wrote:

> Snippet #26 solved my problem, essentially in two lines.  I could not
> write those lines for myself now, and I'm not certain I'll ever be able
> to do so.

I'm going to assume it's the Python code itself that is confusing you,
rather than the newforms API...

The lines you're talking about are the body of the constructor in the 
following code:

class AddSnippetForm (forms.Form):
  def __init__ (self, *args, **kwargs):
    super (AddSnippetForm, self).__init__ (*args, **kwargs)
    self.fields['language'].choices = [('', '----------')] + [(lang.id, 
lang.name) for lang in Language.objects.all()]

The first line of the constructor is calling the constructor of the
AddSnippetForm's base class, which is forms.Form. This is necessary
so that the work done by the Form class's constructor is still
performed, even though the constructor was overridden by the child
class.

For details, see the definition of the super function in <http://
docs.python.org/lib/built-in-funcs.html>. I *think* (though I am
not certain) that this line could also be written as:

  forms.Form.__init__ (self, *args, **kwargs)

The funny *args and **kwargs arguments simply pass any positional
and keyword arguments that the constructor was called with through
to the constructor of the base class.

The second line is changing the 'choices' property of the 'language' 
field of the form. So far so good -- but what is that weird looking 
expression inside the second set of square brackets on the right hand 
side of the assignment?

Weel, it is a list comprehension. List Comprehensions are just a
concice way of evaluating an expression over each member of a list, and
returning a list of the results. That line could be written out in full 
as:

  x = [('', '----------')]
  for lang in Language.objects.all ():
    x.append ((lang.id, lang.name))
  self.fields['language'].choices = x

Or by using the map function, as:

  self.fields['language'].choices = [('', '----------')] + map (lambda lang: 
(lang.id, lang.name), Language.objects.all ())

More details about List Comprehensions can be found:

 * in the Python tutorial
   <http://docs.python.org/tut/node7.html#SECTION007140000000000000000>
 * in the Python Language Reference
   <http://docs.python.org/ref/lists.html#l2h-352>
 * in PEP 202 (where List Comprehensions were originally defined)
   <http://www.python.org/dev/peps/pep-0202/>

When learning any new API/framework along with a language at the
same time, it is often difficult to determine whether something
that you find confusing is a feature of the language, or just an
artifact of the API/framework.

In my experience, once you break down the various little bits
(and know where to find the documentation!), code like this will
start to look a bit less magical and a bit more like, well, regular
code. :)

-- 
Sam Morris
http://robots.org.uk/

PGP key id 1024D/5EA01078
3412 EA18 1277 354B 991B  C869 B219 7FDB 5EA0 1078


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to