Skip to content Skip to sidebar Skip to footer

Scope Of Class Variable With List Comprehension

have a look at the following piece of code: class a: s = 'python' b = ['p', 'y'] c = [x for x in s] the output: >>> a.c ['p', 'y', 't', 'h', 'o', 'n'] but wh

Solution 1:

This isn't so much about the variable scope in list comprehensions as about the way classes work. In Python 3 (but not in Python 2!), list comprehensions don't affect the scope around them:

>>>[i for i inrange(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>i
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'i' is not defined
>>>i = 0>>>[i for i inrange(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>i
0

However, when you do that in a class, it won't look for b among the class attributes the way it would in the local scope of a module or function. To do what you are trying to do, use the @property decorator:

>>>classa:...  s = 'python'...  b = 'py'...  @property...defc(self):...return [x for x in self.s if x in self.b]...>>>A = a()>>>A.c
['p', 'y']

Also, remember that strings are iterable too (they're just lists of their component characters), so no need to explicitly make b a list.

Post a Comment for "Scope Of Class Variable With List Comprehension"