[Python-ideas] Inline assignments using "given" clauses

Ed Kellett e+python-ideas at kellett.im
Fri May 11 07:00:21 EDT 2018


On 2018-05-10 17:10, Gustavo Carneiro wrote:
> IMHO, all these toy examples don't translate well to the real world because
> they tend to use very short variable names while in real world [good
> written code] tends to select longer more descriptive variable names.
> 
> Try replacing "c" with  a longer name, like input_command, then it becomes:
> 
>     while ((input_command = getchar()) != EOF) ...
> 
>     while (input_command = getchar(), input_command != EOF) ...
This thread really isn't about variable naming. I'll simply say that
I've always written the C idiom with `c` as the variable name, I've seen
literally hundreds of others do the same, and I've never seen anyone
spell it `input_command`.

I do the same in Python in a few contexts, usually where the variable's
meaning is very clear from its usage, or where the code in question
doesn't know or care what the variable is used for:

    for i in range(10):
    def get_const(self, x):
    for k in self.defaults.keys():

etc., and if we had `given` clauses, I'd probably do this there too. I
think one of the advantages of that syntax is that it makes it extremely
clear what's going on:

    if m given m = re.match(...):

I don't need to try to stuff an English description of m into its name,
because the `given` clause already describes it perfectly.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180511/ee1b48f8/attachment-0001.sig>


More information about the Python-ideas mailing list