Mystical comma in a Python expression -


this question has answer here:

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

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -