This question is about design and readbility, not performance:
Is it more pythonic to initialize tuples with the tuple keyword, or just with parenthesis? Both do the same thing, but explicitly typing tuple
is more verbose.
Here's two different cases of tuple initalization.
- From existing iterable
def myfunc(myarg: Iterable):
# "tuple" is explicitly typed out
mytuple = tuple(myarg)
def myfunc(myarg: Iterable):
# "tuple" is not explicitly typed out; only parenthesis are used
mytuple = ([myarg])
- Creating new tuple from literal
# "tuple" is explicitly typed out
mytuple = tuple([1, 2, 3, 4])
# "tuple" is not explicitly typed out; only parenthesis are used
mytuple = (1, 2, 3, 4)
Contradictorily, I know that it is more common to initialize lists without explicit;y typing list
:
mylist = [1, 2, 3, 4] # "list" is not explicitly typed out; only brackets are used
# Rather than:
mylist = list([1, 2, 3, 4]) # "list" is explicitly typed out
Personally, I always explicitly type out tuple to make it easier to see that a tuple is being initialized. However, this contradicts my style (which is the more popular style) of initializing lists (without explicitly typing list.
In conclusion, I want to know which way of initializing tuples is more pythonic.