views:

303

answers:

9

I am new to python. I learning this stuff because google app engine doesn't allow php.

Python is touted to be an easy and friendly language to use. But I find trying to tamper with an existing code causes indentation errors.

For example: this piece of code

class SearchThread(Thread):
  def __init__(self, s, q):
   Thread.__init__(self)
    self.s = s
    self.q = q
    self.results = []

produces an error

but

class SearchThread(Thread):
  def __init__(self, s, q):
    Thread.__init__(self)
    self.s = s
    self.q = q
    self.results = []

doesn't give an error!

isn't this a bit silly?

Why python works this way?

Edit: Now I get it! Thanks for clearing this up. I think having spaces is pretty neat. It never struck me that spaces could be used in the place of braces.

+1  A: 

I'm no python guru, but yes, whitespace is a syntactic element. It is unusual in this aspect and differs from most languages such as C in that respect.

Earlz
+1  A: 

No. You indent for new structures, and dedent when done. This is how it's done in most languages; Python just enforces it.

Ignacio Vazquez-Abrams
dedent is a word? lol (btw, I'm not the one to vote you down, I just find that funny)
Earlz
Ex-dent perhaps? Is that a use-to-be dent? :)
GrayWizardx
maybe its opdent?
Woot4Moo
+1 for dedent ;)
SP
if the prefix is `in` shouldn't it be `undent`?
Mica
i would think the opposite of in is out? so outdent? ;-P
Steve Tranby
+7  A: 

Yes, indention IS important in Python. Python uses indention instead of

{ ... }

like C (and the like) or

begin
...
end;

like Pascal

Mef
+6  A: 

Indentation in Python is, simply put, the equivalent of {} in other languages like C++ and C#

McAden
+4  A: 

This is precisely how Python works -- it uses indentation to define a block. All contiguous lines with the same indentation belong to the same block. So when you have one line indented with two spaces and the next with four, those are two separate blocks.

Bryan Oakley
A: 

That is how Python handles things, there is a video lecture from the MIT series on algorithms that makes a bit of a joke at Python's expense. By joke of course I mean he calls Python terrible.

http://www.youtube.com/watch?v=JPyuH4qXLZ0 31:30

Woot4Moo
Provide a link please.
Hamish Grubijan
Do you have a link to said video? =)
Tomas Lycken
one sec I am looking for the exact time
Woot4Moo
Found it 31:30 15 characters
Woot4Moo
+1  A: 

doesn't give an error! isn't this a bit silly?

It seems at first glance, but eventually it will make sense.

Python doesn't use braces or begin/end keyword, instead it relays on the source code indentation.

This serves for two purposes.

  1. Delimit structures
  2. Enhance readability
OscarRyz
A: 

I have to second @Ignacio Most of times you are already doing it.

Python simply uses the fact to its advantage by not having to have matching pairs of '{ .. }' everywhere and just use the implicit indentation that's already present.

This benefits is twofold:

  • You always have at-least a proper block indication
  • You don't have to have { and } and all the problems associated with them

I think good and meaningful indentation is good practice in any languages, including PHP.

chakrit
A: 

Python indentation is completely relevant. It also forces you to make your code readable. Read PEP-8 for the coding style.

A similar feature of python is docstrings. It is always good practice to add comments to document your code, but in python those are also accessible at runtime. Look at list.__doc__ for an example.

Tobu