binding - Python list comprehension rebind names even after scope of comprehension. Is this right? -


list comprehensions having unexpected interactions scoping. expected behaviour?

i've got method:

def leave_room(self, uid):   u = self.user_by_id(uid)   r = self.rooms[u.rid]    other_uids = [ouid ouid in r.users_by_id.keys() if ouid != u.uid]   other_us = [self.user_by_id(uid) uid in other_uids]    r.remove_user(uid) # oops! uid has been re-bound list comprehension above    # interestingly, it's rebound last uid in list, error shows   # when len > 1 

at risk of whining, brutal source of errors. write new code, find weird errors due rebinding -- know it's problem. need make rule "always preface temp vars in list comprehensions underscore", that's not fool-proof.

the fact there's random time-bomb waiting kind of negates nice "ease of use" of list comprehensions.

list comprehensions leak loop control variable in python 2 not in python 3. here's guido van rossum (creator of python) explaining history behind this:

we made change in python 3, improve equivalence between list comprehensions , generator expressions. in python 2, list comprehension "leaks" loop control variable surrounding scope:

x = 'before' = [x x in 1, 2, 3] print x # prints '3', not 'before' 

this artifact of original implementation of list comprehensions; 1 of python's "dirty little secrets" years. started out intentional compromise make list comprehensions blindingly fast, , while not common pitfall beginners, stung people occasionally. generator expressions not this. generator expressions implemented using generators, execution requires separate execution frame. thus, generator expressions (especially if iterate on short sequence) less efficient list comprehensions.

however, in python 3, decided fix "dirty little secret" of list comprehensions using same implementation strategy generator expressions. thus, in python 3, above example (after modification use print(x) :-) print 'before', proving 'x' in list comprehension temporarily shadows not override 'x' in surrounding scope.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -