For review: PEP 308 - If-then-else expression

James J. Besemer jb at cascade-sys.com
Sun Feb 9 05:00:49 EST 2003


Tim Peters wrote:
> [Paul Rubin]
> 
>>...
>>Btw, just the other night I discovered that Python has a feature that
>>I first saw in C: you can enter a list or tuple with an extra comma at
>>the end (like [2,3,5,] , and the extra comma is ignored.
> 
> 
> Also for dicts, and formal and actual argument lists.  A trailing comma in a
> container literal is often used as a clue to the reader that the author
> *expects* the container to grow more stuff over time.  For example, a dict
> mapping selected HTML tags to action routines.  

Interesting new tid-bit of style, one I never heard of before.

I find trailing commas most useful in cases where I'm initializing a list or 
dictionary.  If the list is non-trivial, it usually improves readability and 
aids maintainability to stack the elements vertically.  Always including a 
trailing comma allows elements to be inserted and deleted with impunity, 
having eliminated the need to specially treat the last element.

> For the life of me, though, I've never found a good reason to add a trailing
> comma to a formal argument list.

Same argument as with long list applies to those rare argument lists that 
happen to have many parameters.

def bigfun(
	aalksjf,
	alskfjas,
	aslkfjasdlfjasdfl,
	alsdfjalsdjlkjsa,
	aslkfjsladfjklsaf,
	asdlfjasldjjksadf,
	):

Such lattitude in syntax also is very much appreciated by programs that 
generated source code.

Regards

--jb


-- 
James J. Besemer		503-280-0838 voice
2727 NE Skidmore St.		503-280-0375 fax
Portland, Oregon 97211-6557	mailto:jb at cascade-sys.com
				http://cascade-sys.com	







More information about the Python-list mailing list