[Tutor] challenge-chapter 2

Peter Otten __peter__ at web.de
Wed May 22 09:59:46 CEST 2013


Andrew Triplett wrote:

> I am on chapter two for Python Programming working on the challenges and
> the question is:
> 
> 1. Create a list of legal and illegal variable names. Describe why each is
> either legal or illegal. Next, create a list of "good" and "bad" legal
> variable names. Describe why each is either a good or bad choice for a
> variable name.
> 
> Can you help me solve this problem?

i
n
foo
1timepad
with
__
_name
__name
row1
row2
row3
question
length
x
bottles
number_of_bottles
number_of_bottles_of_beer_on_the_wall
seperate_concerns
next_KindOfQuestion
it's_about_time
Animal
animal
ANIMAL
HttpServer
HTTPServer
bin2hex
bin_to_hex
childs
intNumber
strValue

So here's your lists, but they got totally mixed up. Also, while it's easy 
to separate the legal from the illegal names (by typing them into the 
interpreter) the distinction between good and bad is not so clear-cut.
You really have to think about it and form an opinion. Write it down in a 
sentence or two and post it here.

Hint: Often a name may even be good or bad depending on the context.
For example

for x, y in points:
    r = math.sqrt(x*x + y*y)
    print x, y, "-->", r

Here x is a fine name because if follows a well-known mathematical 
convention...

with open(datafile, "rb") as f:
    x = pickle.load(f)

...whereas this x leaves you totally clueless about what x could contain.


Another hint: One criterium to decide whether a name is "good" is its 
compliance with PEP 8 
<http://www.python.org/dev/peps/pep-0008/#naming-conventions>




More information about the Tutor mailing list