class - Understanding variable scope in Python classes -


this question has answer here:

can please explain why fail:

class a:    = 42    b = list(a + in range(10)) 

it fails because a isn't in namespace generator expression can see.

this works python2 , python3. lambda default argument trick reference of a in namespace generator expression can see.

>>> class a: ...    = 42 ...    b = (lambda a=a:list(a + in range(10)))() ...  >>> a.b [42, 43, 44, 45, 46, 47, 48, 49, 50, 51] 

using list comprehension b clearer in opinion

...    b = (lambda a=a:[a + in range(10)])() 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -