Python Worst Practices

Chris Angelico rosuav at gmail.com
Thu Feb 26 08:37:39 EST 2015


On Fri, Feb 27, 2015 at 12:12 AM, m <mvoicem at gmail.com> wrote:
> W dniu 25.02.2015 21:45, Mark Lawrence pisze:
>>
>> http://www.slideshare.net/pydanny/python-worst-practices
>>
>> Any that should be added to this list?  Any that be removed as not that
>> bad?
>>
>
>
> I disagree with slide 16. If I wanted to use long variable names, I would
> still code in Java.

Clearly you aren't bothered by ambiguities, given that your name is
"m". You're lower-case m, and the James Bond character is upper-case
M... yeah, this isn't going to be a problem, with seven billion people
on the planet!

In case it's not obvious from slide 17, the author is advocating
neither the ridiculously short, nor the ridiculously long. This is a
topic that you could go into great detail on, but a general rule of
thumb is that short names go with short-lived variables, and longer
names go with large-scope variables. [1] So your function names
shouldn't be single letters, but your loop counters can and should be
short:

def discard_all_spam():
    for msg in self.messages:
        if msg.is_spam(): ms.discard()

And of course, the use of "i" as an integer loop index dates back so
far and is so well known that you don't need anything else:

def get_password():
    for i in range(4):
        if i: print("%d wrong tries...")
        s = input("What's the password? ")
        if validate_password(s): return s
    print("Too many wrong tries, go away.")

This isn't Java coding.

ChrisA

[1] Yes, Python doesn't have variables per se. But how else am I
supposed to differentiate between the name and the concept of a name
binding?



More information about the Python-list mailing list