Friday Finking: initialising values and implied tuples

Alan Gauld alan.gauld at yahoo.co.uk
Fri Apr 2 18:42:37 EDT 2021


On 02/04/2021 23:10, dn via Python-list wrote:
> When there are several items to be defined and initialised, how do you
> prefer to format the code, and why?

> (a) basic linear presentation:
> 
> resource = "Oil"
> time = 1
> crude = 2
> residue = 3
> my_list = "long"

In production code I'd almost always go with this one.
It's the only option that both keeps the variable and value
together and allows me to add an explanatory comment if
necessary.

> (e) implicit tuples:
> 
> resource, time, crude, residue, my_list = "Oil", 1, 2, 3, "long"
Occasionally, and in personal code, I'll  use this.
Although this would be about as many items I'd ever
have in one list.

But in production code, if items are related, I'd use it.
eg.
x,y = 42,50   # starting coordinates
hour,min,sec = 0,0,0  # default start time

But in both those cases I'd more likely define a variable
to be a tuple:
eg.
start = (42,50)  # starting coordinates

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Python-list mailing list