The thing that attracts me the most about programming is how you can make a certain code shorter by using clever logic or clever syntaxes. Through “Trick of the Day,” I can hopefully share a trick that I think is clever during my work (which involves quite a bit of programming).
Today’s trick is a very clever one in that if you have an instance such as the following:
1 |
a, b, c, d, e = True, True, True, True, True |
There just has to be a way to shorten that repetition. Fortunately in Python, a trailing comma notation can be used to create a one-term tuple. This is called a sequence type.
So we could write the above code as:
1 |
a, b, c, d, e = (True,)*5 |
I particularly find myself heavily using this when creating multiple objects of the same class and that requires the same inputs.
If you have any other way you perform this, feel free to share your thoughts below.