Python Newbie

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Feb 22 11:26:06 CET 2013


On Thu, 21 Feb 2013 14:40:37 -0800, piterrr.dolinski wrote:

> With Python, I am having to do this
> 
> code
> code
> 
> ##############################
> 
> if (some condition):
>   code
>   code
> 
> ##############################


I prefer this:


# WARNING WARNING WARNING IF STATEMENT AHEAD BEWARE BEWARE BEWARE
if something():
    do(stuff)
# WHEW IF STATEMENT FINISHED!!! WE SURVIVED, THANK THE GODS!!!


That way I can be sure that people reading it will know it is an "if" and 
not a "for" or "while", just in case the actual code wasn't clear enough.



> I am nervous about using variables "out of the blue", without having to
> declare them.

I feel your pain. I'm nervous about declaring variables "out of the 
blue", without having to declare that I'm about to declare them. I live 
in hope that some day I will find a language that lets me write:

FORWARD VARS:
    int x;
    double y;
    str s;

VARS:
    int x;
    float y;
    str s;


and have the compiler catch my error in declaring y as a float instead of 
a double. Then I will be one step closer to my dream of not having to 
understand the code I write.


> For example, when I write "i = 0" it is perfectly OK to
> Python without 'i' being declared earlier. How do I know that I haven't
> used this variable earlier and I am unintentionally overwriting the
> value? I find I constantly have to use the search facility in the
> editor, which is not fun.

Some people suggest that functions should be small enough to read over in 
a glance, or at least a couple of glances. They even say that variable 
names should be meaningful, so you can tell whether a variable has been 
used from context. I say, fi to that! Programming should be a challenge! 
It should be exciting! Which is why I never use functions, and always use 
the same single-letter variable with a cryptographic hash to distinguish 
them.

i048b8497cf86dab9dade2ce6beddf13a = []
i048b6497cd85d9b9da2a3ce6bdedf167 = 42


*wink*


I'm not going to apologise for taking the piss, although I do hope you 
will take it in the spirit it is intended: constructive criticism rather 
than abuse. The reality is, these "problems" you are running into are not 
problems in practice, and the "solutions" you are used to, like braces, 
are visually noisy and not terribly effective. You would probably think 
differently of { } if they were spelled "BEGIN" and "END" like in Pascal.

Braces have one, and only one, advantage: if you pass code through a 
noisy environment that deletes whitespace at will, you can recover the 
structure of the code from the braces. Python does not have that 
advantage, and sometimes it is a pain, e.g. when people email code using 
a broken mail client that deletes spaces.

But then, if you passed C code through something that deleted the braces, 
you'd be royally screwed too. The solution to that is to stop having 
tools that think that whitespace isn't significant. Of course it is 
significant, it is significant to the human reader, who is about a 
hundred billion trillion times more important than the compiler.



-- 
Steven



More information about the Python-list mailing list