CCing the list.
Always use ReplyAll or ReplyList when responding to the list.

On 05/08/17 18:17, Xiaosong Chen wrote:
> 2) Also we avoid importing with the
>> from foo import *
>>
>> even if you import with
>>
>> import foo
>>
>> foo.temp can still be accessed. When you use autocomplete in an
>> interactive shell like ipython, it might become an item.

foo.temp does not pose a problem of name pollution because you have
to prefix it with foo. Thus every module can have a temp variable and
there is no risk of names clashing because they all need to be
accessed with name.temp. You can even compare them:

if foo.temp < bar.temp....

> 3) We need fewer temporary variables because we can use
>> tuple unpacking and generator expressions to replace many
>> scenarios where C would use a temporary variable.
> I know little with python's xxx comprehension. In my memory, temporary
> variables seem not allowed inside, and the same for lambda
> expressions. Might you give some examples?
>
Things like list comprehensions often replace explicit loops which
would introduce extra named variables. Consider:

>>> lst = [n for n in range(7)]
>>> n
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'n' is not defined
>>> lst
[0, 1, 2, 3, 4, 5, 6]
>>> lst2 = []
>>> for n in range(7): lst2.append(n)
...
>>> n
6
>>> lst2
[0, 1, 2, 3, 4, 5, 6]
>>>

You can see how in the list comprehension case n
is not visible outside the comprehension whereas n
exists outside the for loop(as you noted in your original
post) so, by using the comprehension we reduce
the number of visible names.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to