[Tutor] Misc question about scoping

Tino Dai oberoc at gmail.com
Fri Jun 4 14:46:20 CEST 2010


> That is technically correct, you could do that. That's a good example of
> the syntax of the `if` expression, but it's a bad example of where to
> use it:
>
> (1) it only works in Python 2.5 or better; and
>
> (2) experienced Python programmers will laugh at you :)
>
> with all due respect to Alan who suggested it. It really is an
> unnecessarily complicated and verbose way of doing a simple assignment,
> nearly as bad as writing this:
>
> if len(mystring) == 0:
>    n = 0
> else:
>    n = len(mystring)
>
>
> instead of just
>
> n = len(mystring)
>
>
Hey Everybody,

        Thank you for the rich discussion and making me a better
python programmer! Between the new topics that I have learned in
Python and Django from documentation, experimentation, and this list,
I think my IQ has gone up a couple of points!

        I'm at a point where I can do most things in Python (maybe) ,
now I'm looking to do them succinctly and elegantly. For instance, I
had about 10 - 15 lines of code to do this before with a bunch of
loops and if blocks, I distilled the product down to this:

       answerDict=dict(map(lambda x: (str(x[1]),x[0]),map(lambda x: \
          x.values(),Answer.objects.filter(fk_questionSet=1). \
          filter(fk_question=1).values('widgetAnswer').order_by(). \
          annotate(widgetCount=Count('widgetAnswer')))))

So instead of my python code doing the "leg work", I have the Django
ORM and that underlying DB do the work. Also, I leveraged lambda
functions and maps to coerce the data in to the right format. Pretty
cool IMHO. And I turn it over to the group to see if there is any
improvements or gotchas that I missed.

Thanks and Thanks in advance,
Tino


More information about the Tutor mailing list