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
Post a Comment