The syntax of declaring tuples with commas is clear and everywhere I saw it it is being used with literals enclosed in braces e.g. (1,).
However, python does allow to use commas without enclosing braces to declare tuples, and there is strange behavior in one particular case, see the code below.
def ifElseExpressionTrailingComma():
return 1 if True else 0,
def ifElseExpressionTrailingCommaWrapped():
return 1 if True else (0,)
print ifElseExpressionTrailingComma()
print ifElseExpressionTrailingCommaWrapped()
Output:
(1,) # what??
1
Tested on 2.7 and 3.5. Can someone explain why 1 is implicitly converted to the tuple?
Aucun commentaire:
Enregistrer un commentaire