<div dir="ltr"><br><div class="gmail_extra"><div class="gmail_quote">On 24 February 2013 19:29, <span dir="ltr"><<a href="mailto:piterrr.dolinski@gmail.com" target="_blank">piterrr.dolinski@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="im"><span style="color:rgb(34,34,34)">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.</span><br>
</div>
<br>
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.<br>
<br>
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.</blockquote><div><br></div><div style>If you can't remember what type a variable is, you're doing something incorrectly.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">The redundant comments serve the same purpose.</blockquote>
<div><br></div><div style>To help you remember the type of the variable?</div><div style><br></div><div style><span style="font-family:arial,sans-serif;font-size:10px">> intX = 32 # decl + init int var</span></div>
<div style>How is it not obvious that "intX" is an integer *without* the comment?</div><div style><br></div><div style>> X = 32</div><div style>How is it not obvious that X is an integer?</div><div style><br style="font-family:arial,sans-serif;font-size:10px">
<span style="font-family:arial,sans-serif;font-size:10px">> intX_asString = None # decl + init with NULL string var</span></div><div style>How is it not obvious that intX_asString is an integer (it starts with "int", duh"... oh wait)</div>
<div style><br></div><div style>The comment says it's the NULL string, so it's "". F*ck.</div><div style><br></div><div style>It's None. Why? No idea.</div><div style><br></div><div style><span style="font-family:arial,sans-serif;font-size:10px">> intX_asString = intX.__str__ () # convert int to string</span><br>
</div><div style><font face="arial, sans-serif">Wait. So why'd you write the previous line?</font></div><div style><div><br></div><div>Just write:</div><div>> X_as_string = str(X)</div><div>'Cause "str" *always* returns a string. So it's a string. How is that not obvious?</div>
<div><br></div><div><br></div><div style>But then, what's the context?</div><div style><br></div><div style>"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.</div>
</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">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.<br>
</blockquote><div><br></div><div style>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.</div>
<div style><br></div><div style><br></div><div style>For example:</div><div style><div>def floatA_floatB_floatC_to_tupleR(floatA, floatB, floatC): # decl with floatA, floatB, floatC parameters</div><div> floatD = None # decl + init with NULL float var</div>
<div> floatD = ((floatB ** 2) - (4 * floatA * floatC)) # set to B² - 4AC</div><div> floatD = floatD ** 0.5 # set to √floatD</div><div><br></div><div> floatR1 = None # decl + init with NULL float var</div><div> floatR1 = (((- floatB) + floatD) / (2 * floatA)) # set to (-B+D)/(2A)</div>
<div><br></div><div> floatR2 = None # decl + init with NULL float var</div><div> floatR2 = (((- floatB) - floatD) / (2 * floatA)) # set to (-B-D)/(2A)</div><div><br></div><div> return (floatR1, floatR2)</div><div>
<br></div><div style>Versus</div><div><br></div><div>def solve_quadratic(a, b, c):</div><div><span class="" style="white-space:pre"> </span>"""Solve a quadratic equation of the form ax² + bx + c = 0</div><div>
<br></div><div><span class="" style="white-space:pre"> </span>The result will be a tuple of the two results; the results can be equal if the determinant is 0.</div><div><span class="" style="white-space:pre"> </span>This supports imaginary results for if the determinant is negative."""</div>
<div><br></div><div><span class="" style="white-space:pre"> </span># The method used is the quadratic equation:</div><div><span class="" style="white-space:pre"> </span># <a href="http://en.wikipedia.org/wiki/Quadratic_equation">http://en.wikipedia.org/wiki/Quadratic_equation</a></div>
<div><br></div><div><span class="" style="white-space:pre"> </span># b² - 4ac</div><div><span class="" style="white-space:pre"> </span>determinant = b**2 - 4*a*c</div><div><br></div><div><span class="" style="white-space:pre"> </span># ±√(b² - 4ac)</div>
<div><span class="" style="white-space:pre"> </span>sqrt_determinant = determinant ** 0.5</div><div><span class="" style="white-space:pre"> </span>squareroots = sqrt_determinant, -sqrt_determinant</div><div><br></div><div>
<span class="" style="white-space:pre"> </span># -b ± √(b² - 4ac)</div><div><span class="" style="white-space:pre"> </span>fraction_tops = [(-b + d) for d in squareroots]</div><div><br></div><div><span class="" style="white-space:pre"> </span>results = [top/(2*a) for top in fraction_tops]</div>
<div><br></div><div><span class="" style="white-space:pre"> </span>return results</div><div><br></div><div style>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.</div>
<div style><br></div><div style>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.</div>
<div style><br></div><div style>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.<br></div></div></div></div>
</div>