Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python Newbie

Reply
Thread Tools

Python Newbie

 
 
Chris Angelico
Guest
Posts: n/a
 
      02-24-2013
On Mon, Feb 25, 2013 at 7:34 AM, Ethan Furman <(E-Mail Removed)> wrote:
> One of the things I love about Python is its ability to get out of the way
> and let me work:
>
> - no variable declarations, just use 'em
> - no type declarations, just use 'em
> - no need to remember what's an object and what's not -- everything is an
> object
> - no need to cast to bool as everything has a truthy/falsey (something vs
> nothing) value


Variable declarations can go either way; Python requires you to name
all globals that you mutate, and to be careful when working with
nested functions. With declared variables, you name all locals, and
can enforce scoping and destructors without language features like
'with'. Both options are viable.

I absolutely agree with your third point. Treat 'em all as objects!
But of *course*, Java is "more object oriented" than Python. Everyone
knows that.

ChrisA
 
Reply With Quote
 
 
 
 
Roy Smith
Guest
Posts: n/a
 
      02-24-2013
In article <(E-Mail Removed)>,
Chris Angelico <(E-Mail Removed)> wrote:

> > no need to remember what's an object and what's not -- everything is an
> > object


Well, not quite everything. If I write:

if foo:
do_this()
and_this()

the code block making up the body of the "if" statement is not an
object. In some languages, it is.
 
Reply With Quote
 
 
 
 
Joshua Landau
Guest
Posts: n/a
 
      02-24-2013
On 24 February 2013 19:29, <(E-Mail Removed)> 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.

 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      02-24-2013
On Mon, Feb 25, 2013 at 8:08 AM, Roy Smith <(E-Mail Removed)> wrote:
> In article <(E-Mail Removed)>,
> Chris Angelico <(E-Mail Removed)> wrote:
>
>> > no need to remember what's an object and what's not -- everything is an
>> > object


Careful on the citations - Ethan Furman said that, I just quoted him.

> Well, not quite everything. If I write:
>
> if foo:
> do_this()
> and_this()
>
> the code block making up the body of the "if" statement is not an
> object. In some languages, it is.


Maybe, but the code of an entire function *is*. Granted, it's not an
object that can be built up manually (at least, not that I know of),
and it offers only limited functionality (dis.dis, but not a lot
else), so really it could be seen as just an implementation detail of
the function object itself. But it's still an object.

ChrisA
 
Reply With Quote
 
Joshua Landau
Guest
Posts: n/a
 
      02-24-2013
On 24 February 2013 20:48, Roy Smith <(E-Mail Removed)> wrote:

> In article <(E-Mail Removed)>,
> Chris Angelico <(E-Mail Removed)> wrote:
>
> > On Mon, Feb 25, 2013 at 7:34 AM, MRAB <(E-Mail Removed)>

> wrote:
> > > Some languages require parentheses, others don't.
> > >
> > > C does. C++, Java and C# are descended from, or influenced by, C.
> > >
> > > Algol didn't (doesn't?). Pascal, Modula-2, Oberon, Ada, and others
> > > don't.
> > >
> > > Parentheses are used where required, but not used where they're not
> > > required, in order to reduce visual clutter.

> >
> > And just to muddy the waters, parens are used in Python when the
> > condition goes over a line break:
> >
> > if (condition1
> > and condition2
> > and condition3):
> >
> > ChrisA

>
> That could also be written:
>
> if condition1 \
> and condition2 \
> and condition3:
>
> but as a practical matter, I would write it in the parens style, if for
> no other reason than because emacs does a better job of auto-indenting
> it that way
>


Pah,

condition1 = long_condition_expression_1
condition2 = long_condition_expression_2
condition3 = long_condition_expression_3

if condition1 and condition2 and condition3:
STUFF

No multiline needed. If you have *many* conditions, then:

supercondition = all(
condition1,
condition2,
condition3,
condition4,
condition5,
condition6,
condition7,
condition8,
condition9
) # or equiv.

if supercondition:
STUFF


Reason:
Indentation should be *really simple*.

 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      02-24-2013
On Mon, Feb 25, 2013 at 8:35 AM, Joshua Landau
<(E-Mail Removed)> wrote:
> 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."""
> ...
> results = [top/(2*a) for top in fraction_tops]


Yeah, I think we know which one is the more readable... Just to
nit-pick a little though, that returns a list when its docstring says
it'll return a tuple

Other than that (which is probably better solved by changing the docs
than the code), the only change I'd make would be to ditch the
fraction_tops temporary (and to throw out some of the comments that
serve only to reexpress the code that immediately follows them, though
for a demo they're entirely appropriate). Even in a language with
mandatory declarations, the code would look pretty similar:

# Assume that the declaration 'complex' permits a float - otherwise
you need a Pike-style piped declaration eg "float|complex"
# Details elided for brevity, keep the docstring and comments from the
above version
list(complex) solve_quadratic(float a, float b, float c):
float determinant = b**2 - 4*a*c
complex sqrt_determinant = determinant ** 0.5
tuple(complex) squareroots = sqrt_determinant, -sqrt_determinant
return [(-b + d)/(2*a) for top in squareroots]

Variable names seldom if ever need to identify their types, if by
"type" you mean what the language sees as a type. There are times when
it's useful to adorn a variable name with a unit, perhaps (length_ft
and height_m shouldn't be multiplied together), or some other special
marker (a "tainted" flag on all data that's come from user input, and
which therefore must not be executed or interpolated into SQL or
anything), but this is a much higher level of abstraction.

ChrisA
 
Reply With Quote
 
Ethan Furman
Guest
Posts: n/a
 
      02-24-2013
On 02/24/2013 12:58 PM, Chris Angelico wrote:
> On Mon, Feb 25, 2013 at 7:34 AM, Ethan Furman <(E-Mail Removed)> wrote:
>>
>> - no variable declarations, just use 'em

>
> Variable declarations can go either way; Python requires you to name
> all globals that you mutate


I'm not sure what you mean -- example?

--
~Ethan~
 
Reply With Quote
 
Mitya Sirenef
Guest
Posts: n/a
 
      02-24-2013
On 02/24/2013 04:44 PM, Chris Angelico wrote:
> On Mon, Feb 25, 2013 at 8:08 AM, Roy Smith <(E-Mail Removed)> wrote:
>> In article <(E-Mail Removed)>,
>> Chris Angelico <(E-Mail Removed)> wrote:
>>
>>>> no need to remember what's an object and what's not -- everything

is an
>>>> object

>
> Careful on the citations - Ethan Furman said that, I just quoted him.
>
>> Well, not quite everything. If I write:
>>
>> if foo:
>> do_this()
>> and_this()
>>
>> the code block making up the body of the "if" statement is not an
>> object. In some languages, it is.

>
> Maybe, but the code of an entire function *is*. Granted, it's not an
> object that can be built up manually (at least, not that I know of),
> and it offers only limited functionality (dis.dis, but not a lot
> else), so really it could be seen as just an implementation detail of
> the function object itself. But it's still an object.
>
> ChrisA



But if block doesn't have to be inside a function, right? It needs
to be inside a module, but then again everything is inside a module, but
it wouldn't be very object-oriented if the module was the only object in
Python .

-m


--
Lark's Tongue Guide to Python: http://lightbird.net/larks/

The press, the machine, the railway, the telegraph are premises whose
thousand-year conclusion no one has yet dared to draw. Friedrich Nietzsche

 
Reply With Quote
 
piterrr.dolinski@gmail.com
Guest
Posts: n/a
 
      02-24-2013
Josh,

Not thank you for your malicious post.
I think you are missing the point here.

My source code was just a dummy to offer context for the question I wanted to ask. Further down the line, if I ever feel I don't need to pseudo-declare variables I will stop doing it. But for the moment I am trying to imitate familiar ground.

My code as written has no syntax errors, so what's the problem? It is highly unlikely you will ever read any of my Python code - no need to get excited over a few of my lines.

And you don't need to answer questions which were not posed, thank you.

I wanted Python to register what type of variable I'm after. So I init my vars accordingly, int might be 0, float 0.0 and string with null, err... None.

In practice, I wouldn't define an intX_asString var, I would do "str (num)" every time a string representation is needed, provided it isn't a loop, as in that context the expression would probably negatively impact performance in an interpreted language.

Peter
 
Reply With Quote
 
piterrr.dolinski@gmail.com
Guest
Posts: n/a
 
      02-24-2013
Josh,

Not thank you for your malicious post.
I think you are missing the point here.

My source code was just a dummy to offer context for the question I wanted to ask. Further down the line, if I ever feel I don't need to pseudo-declare variables I will stop doing it. But for the moment I am trying to imitate familiar ground.

My code as written has no syntax errors, so what's the problem? It is highly unlikely you will ever read any of my Python code - no need to get excited over a few of my lines.

And you don't need to answer questions which were not posed, thank you.

I wanted Python to register what type of variable I'm after. So I init my vars accordingly, int might be 0, float 0.0 and string with null, err... None.

In practice, I wouldn't define an intX_asString var, I would do "str (num)" every time a string representation is needed, provided it isn't a loop, as in that context the expression would probably negatively impact performance in an interpreted language.

Peter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(Python newbie) Using XP-SP2/MSVC6: No Python24_d.lib, winzip barfs on Python-2.4.1.tar, cannot download bzip2 Bill Davy Python 12 04-22-2005 12:19 PM
Re: (Python newbie) Using XP-SP2/MSVC6: No Python24_d.lib,winzip barfs on Python-2.4.1.tar, cannot download bzip2 Jaime Wyant Python 0 04-21-2005 12:49 PM
RE: (Python newbie) Using XP-SP2/MSVC6: No Python24_d.lib,winzip barfs on Python-2.4.1.tar, cannot download bzip2 Bill Davy Python 0 04-19-2005 09:27 AM
Python Newbie. Python on PPC, please assist. pythonnewbie Python 5 10-12-2004 05:51 PM
Creating an OLE server document in Python (MFC/OLE/COM/Python newbie) Drew Pihera Python 0 02-04-2004 07:48 PM



Advertisments