2to3 refactoring [was Re: Tuple parameter unpacking in 3.x]

Aaron "Castironpi" Brady castironpi at gmail.com
Sun Oct 5 09:26:14 EDT 2008


Steven D'Aprano wrote:
> PEP 3113 offers the following recommendation for refactoring tuple 
> arguments:
> 
> def fxn((a, (b, c))):
>     pass
> 
> will be translated into:
> 
> def fxn(a_b_c):
>     (a, (b, c)) = a_b_c
>     pass
> 
> and similar renaming for lambdas. 
> http://www.python.org/dev/peps/pep-3113/
> 
> 
> I'd like to suggest that this naming convention clashes with a very 
> common naming convention, lower_case_with_underscores. That's easy enough 
> to see if you replace the arguments a, b, c above to something more 
> realistic:
> 
> def function(vocab_list, (result, flag), max_value)
> 
> becomes:
> 
> def function(vocab_list, result_flag, max_value)
> 
> Function annotations may help here, but not everyone is going to use them 
> in the same way, or even in a way that is useful, and the 2to3 tool 
> doesn't add annotations.
> 
> It's probably impossible to avoid all naming convention clashes, but I'd 
> like to suggest an alternative which distinguishes between a renamed 
> tuple and an argument name with two words:
> 
> def function(vocab_list, (result, flag), max_value):
>     pass
> 
> becomes:
> 
> def function(vocab_list, t__result_flag, max_value):
>     result, flag = t__result_flag
>     pass
> 
> The 't__' prefix clearly marks the tuple argument as different from the 
> others. The use of a double underscore is unusual in naming conventions, 
> and thus less likely to clash with other conventions. Python users are 
> already trained to distinguish single and double underscores. And while 
> it's three characters longer than the current 2to3 behaviour, the length 
> compares favorably with the original tuple form:
> 
> t__result_flag
> (result, flag)
> 
> What do people think? Is it worth taking this to the python-dev list?
> 
> 

There's the possibility that the most important words should go first in 
this case:

result_flag__t

But, I'll admit that other people could have learned different orders of 
scanning words than I, especially depending on their spoken language 
backgrounds.  A poll of the newsgroup isn't exactly academically 
impartial sampling, but there aren't any better ways to make decisions, 
are there?  (I think it would be easy to make either one a habit.)

Here's the other option in the same context:

def function(vocab_list, result_flag__t, max_value):
     result, flag = result_flag__t
     pass

To be thorough, there's also a trailing double underscore option.

def function(vocab_list, result_flag__, max_value):
     result, flag = result_flag__
     pass

Which I don't recognize from any other usages, but I defer.  If there 
aren't any, conditionally, I think this is my favorite.




More information about the Python-list mailing list