Mystical comma in a Python expression -
this question has answer here:
- python code. comma operator? 2 answers
i wondering comma in code:
line, = the following example shows behavior:
import numpy np import matplotlib.pyplot plt fig = plt.figure() ax = plt.axes(xlim=(0, 2), ylim=(-2, 2)) line, = ax.plot([], [], lw=2) print("first:") print(line) print("second:") line = ax.plot([], [], lw=2) print(line) result:
first: line2d(_line0) second: [<matplotlib.lines.line2d object @ 0xb137c4c>] it becomes confusing when try use line variable, , don't know whether use trailing comma or not:
def init(): line.set_data([], []) return line, or there better way avoids comma?
it's unpacking 1-tuple. eg:
line, = ('foo',) # same line = 'foo' in example compare with
line1, line2 = ('foo', 'bar') # unpack 2-tuple etc.
Comments
Post a Comment