Python supports parallel assignment like
>>> lang, version = "python", 2.7 >>> print lang, version python 2.7
values are assigned to each variable without any issues.
>>> x, y, z = 1, 2, x + y Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'x' is not defined
First python tries to evaluate
x + y expression. Since
x, y is defined in same line, python is unable to access the variable
NameError is raised.
>>> x, y = 1, 2 >>> z, a = x + y, 65 >>> print x, a 1 65
In above code
x, y is referenced before so
x + y is evaluated and the value is assigned to
So don’t assign the values in same line and use it in expression
- Five reasons to use Py.test
- Build Plugins with Pluggy
- Render local images in datasette using datasette-render-local-images
- Parameterize Python Tests
- “Don’t touch your face” - Neural Network will warn you
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.