Python Newbie

Joshua Landau joshua.landau.ws at gmail.com
Sun Feb 24 16:35:31 EST 2013


On 24 February 2013 19:29, <piterrr.dolinski at gmail.com> wrote:

> Hi. Steve, I don't know where you have been over the past couple of days
> but it is widely known (if the thread title is any indication) that I am
> indeed very new to Python, but not new to programming in general.
>
> To give a bit of background where I found __str__, I am using a Python IDE
> called PyScripter. Its Intellisense is full of methods starting and ending
> with "__", hence the question.
>
> Regarding Hungarian notation, I don't use it in any other language but
> Python and JS. Call it poor, but it helps me to remember what type a
> variable is.


If you can't remember what type a variable is, you're doing something
incorrectly.


> The redundant comments serve the same purpose.


To help you remember the type of the variable?

> intX = 32                          # decl + init int var
How is it not obvious that "intX" is an integer *without* the comment?

> X = 32
How is it not obvious that X is an integer?

> intX_asString = None               # decl + init with NULL string var
How is it not obvious that intX_asString is an integer (it starts with
"int", duh"... oh wait)

The comment says it's the NULL string, so it's "". F*ck.

It's None. Why? No idea.

> intX_asString = intX.__str__ ()    # convert int to string
Wait. So why'd you write the previous line?

Just write:
> X_as_string = str(X)
'Cause "str" *always* returns a string. So it's a string. How is that not
obvious?


But then, what's the context?

"X" is a *useless* name. Why are you converting X to a string? I have no
idea. The problem with the code isn't that you could be overwriting "X".
The problem is that your code is contradictory, pretends it's C, has
useless names and doesn't try to be readable.


> As for "pointless predeclaration", it helps me know where in the code I
> first started using the variable, so I know there are no references to it
> before then.
>

Why? Why can't you overwrite old variables? Why can't a variable change
type? If your functions are so large that you're likely to lose track of
what's defined, you have a different problem indeed.


For example:
def floatA_floatB_floatC_to_tupleR(floatA, floatB, floatC): # decl with
floatA, floatB, floatC parameters
    floatD = None # decl + init with NULL float var
    floatD = ((floatB ** 2) - (4 * floatA * floatC)) # set to B² - 4AC
    floatD = floatD ** 0.5 # set to √floatD

    floatR1 = None # decl + init with NULL float var
    floatR1 = (((- floatB) + floatD) / (2 * floatA)) # set to (-B+D)/(2A)

    floatR2 = None # decl + init with NULL float var
    floatR2 = (((- floatB) - floatD) / (2 * floatA)) # set to (-B-D)/(2A)

    return (floatR1, floatR2)

Versus

def solve_quadratic(a, b, c):
"""Solve a quadratic equation of the form ax² + bx + c = 0

The result will be a tuple of the two results; the results can be equal if
the determinant is 0.
This supports imaginary results for if the determinant is negative."""

# The method used is the quadratic equation:
# http://en.wikipedia.org/wiki/Quadratic_equation

# b² - 4ac
determinant = b**2 - 4*a*c

# ±√(b² - 4ac)
 sqrt_determinant = determinant ** 0.5
squareroots = sqrt_determinant, -sqrt_determinant

 # -b ± √(b² - 4ac)
fraction_tops = [(-b + d) for d in squareroots]

results = [top/(2*a) for top in fraction_tops]

return results

Which is easier to read? Reading through it you don't just suddenly forget
what the type of "determinant" is (which must be a number because it's a
determinant) or "results" (which is a container since it's plural). The
names tell you.

The useful comments such as "The method used is..." and "±√(b² - 4ac)" give
you context, too, which is a lot more than can be said of
"floatA_floatB_floatC_to_tupleR". For that, I tried to emulate what I saw
in your code.

I'm not a good programmer. But because of that the code I write makes
sense, so I can understand it. Tell the reader what they want to know, not
what they see.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130224/aa615d8c/attachment.html>


More information about the Python-list mailing list