Python Worst Practices

Dan Sommers dan at tombstonezero.net
Sat Feb 28 02:50:17 EST 2015


On Sat, 28 Feb 2015 17:36:44 +1100, Steven D'Aprano wrote:

> Dan Sommers wrote:

>> And thank goodness for that!  I've been writing Python code since
>> 1997 and version 1.5.<something>,¹ and I still do a double take when
>> emacs colors all my ids that faint blue that means "builtin."

> Although it is not helpful for people using screen-readers, and may be
> of limited use to the colour-blind, I am in favour of colourising
> built-ins so they stand out.

Now if only emacs were clever enough *not* to colorize "id" when it's
one of my names and not the builtin...  ;-)

> On the other hand, I recall seeing an editor which rejected the idea
> of colour-coding built-ins, keywords etc., instead it coloured your
> own variables. So given:
> 
>     spam = 23
>     eggs += cheese*len(sausage)
> 
> spam, eggs, cheese and sausage would be different colours. The idea
> being, when scanning a large code base, all the places that use a
> specific variable would stand out ("just look for the dark green
> word").

As a mostly visual person, I can see (pun intented) the logic and the
value in that.  I wonder how many variables could be easily
distinguished, though, before running out of easily distinguishable
colors.  Then again, a clever underlying algorithm might choose colors
based on *dissimilarity* of the identifiers, so that "i" and "j" would
be very diffent colors, but "spam" and "throat_warbler_mangrove" could
be the same color because they look so different anyway.

>> I don't think I've ever used the builtin function id in a program.
>> Ever.  Not even once.  Honestly, what is a valid use case? 
> 
> Here's one. I think it's the only time I have seen id() used apart from
> interactive experimentation:
> 
> https://code.activestate.com/recipes/577504

Hah.  Very nice.  That sort of thing is probably useful for detecting
self-referential objects, too (e.g., to prevent infinite output for a
circular list).

>> ¹ No, not continuously.  I have eaten and slept since then.
> 
> Slacker!

Sorry.  I'll make up the hours later, I promise!



More information about the Python-list mailing list