With this sample...
#!/usr/bin/python
class MyClass(object):
y = 1
pdq = y
q = 'abc{}'.format(y)
z = [_.format(y) for _ in ['a: {}',
'b: {}',
'c: {}']
]
def __init__(self, val):
self.val = val
In python2
and python3
, MyClass('x').q
gets set properly.
In python2.7
, MyClass('x').z
gets populated properly. It crashes on import in python3
, unable to find y
inside the comprehension. I'm kind of lost as to why - is Python3
just more aggressive unrolling the comprehension during compile?
Python2
> ipython2
Python 2.7.17 (default, Nov 7 2019, 10:07:09)
In [1]: from tester import MyClass
In [2]: MyClass(123)
Out[2]: <tester.MyClass at 0x7f6a7cb79b90>
In [3]: MyClass(123).val
Out[3]: 123
In [4]: MyClass(123).z
Out[4]: ['a: 2', 'b: 2', 'c: 2']
Python3
> ipython3
Python 3.7.5 (default, Nov 7 2019, 10:50:52)
In [1]: from tester import MyClass
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-1-718df2f3bae2> in <module>()
----> 1 from tester import MyClass
tester.py in <module>()
1 #!/usr/bin/python3
----> 2 class MyClass(object):
3 x = 1
4 y = 2
5 pdq = y
tester.py in MyClass()
5 pdq = y
6 q = 'abc{}'.format(y)
----> 7 z = [_.format(y) for _ in ['a: {}',
8 'b: {}',
9 'c: {}']
tester.py in <listcomp>(.0)
5 pdq = y
6 q = 'abc{}'.format(y)
----> 7 z = [_.format(y) for _ in ['a: {}',
8 'b: {}',
9 'c: {}']
NameError: name 'y' is not defined