Help building a dictionary of lists
Joshua Landau
joshua.landau.ws at gmail.com
Mon Nov 12 19:04:16 EST 2012
On 12 November 2012 22:26, NJ1706 <nickj1706 at googlemail.com> wrote:
> # List of tests
> TestList = (
> 'Test_1',
> 'Test_2'
> )
Note that TestList is a *tuple*, not a list.
You normally would want to write "test_names" instead of "TestList" for
several reasons:
* Unless it's a class, Python prefers lowercase_names_with_underscores (or
sometimes camelCaseNamesThatLookLikeThis)
* Python tries not to care what type you have - that your names are in a
list is less important than that you can loop over it
* It's definitely not a list. A list would look like this:
TestList = [
'Test_1',
'Test_2'
]
Additionally, your comment here is somewhat pointless. The code says
*exactly* what your comment does, so why write it twice?
# List of tests
Don't need
# Initialise the dictionary of lists
Don't need
# Loop through the list of tests
Don't need
# Append to the list for each instance
Not very helpful. If you comment here, you should explain *why* you are
looping, not *that* you are looping. You write "append", but *what* and,
more importantly,*why*?
# Initialise our string list
Don't need
# Build string list
Again, whilst it is nice to know more, you are not saying *what*
you are building. What is the *purpose* of the list?
# Convert to string
Don't need
# Assign to target list
Don't need
A good comment might just be one at the start that explains the goal, or
one inside that explains the *point* of a piece of code. Consider these two
commented pieces of code:
# Define a function that takes a, b and c
def f(a, b, c):
# Set s to ((the square of b) minus four times a times c) all to the
power of 0.5
s = (B**2 - 4 * a * c) ** 0.5
# Return the calculated values
return (- b + s) / (2 * a), (- b - s) / (2 * a)
Vs.
# Finds x where the equation "ax**2 + bx + c" makes 0
def f(a, b, c):
# Use the quadratic equation (
http://en.wikipedia.org/wiki/Quadratic_equation)
# s is the square root part of the equation (where x**0.5 is the square
root of x)
s = (B**2 - 4 * a * c) ** 0.5
# Return two values because there are two possible solutions
return (- b + s) / (2 * a), (- b - s) / (2 * a)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121113/4516a134/attachment.html>
More information about the Python-list
mailing list