[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Python intention (Was: bash vs. python scripts - which one is better?)



Manon Metten wrote:
> Than, probably I didn't understand it correct. I thought of it as some 
> prefixed indentation. I like eg. to indent with two spaces and not four or
> six. But then I consequently stick to it. If that's what you mean, then it
> ain't no problem for me.

    It is but it isn't.  Take my previous example of code.  There are 3 blocks
in it.  The indention tells both the human and the interpretor which block is
which.

import os
for file in os.listdir('.'):
    root, ext = os.path.splitext(file)
    if ext.lower() == 'wav':
        mp3 = root + '.mp3'
        result = os.system("lame -h -b 160 '%s' '%s'" % (file, mp3))
        if result:
            print '%s not converted' % file

    I chose 4 spaces because that is the standard of the Python community.
You can do 2, as I did when I converted from Perl to Python, and as long as
you're consistent then you'll have no problems in your code.  You will have
some minute problems importing code intended with 4 spaces but it really is
trivial to fix.  On the other hand switching to 4 spaces makes it uniform and
I have found that the reasons I used 2 in Perl don't occur much in Python
though that might be more a function of my experience resulting in more
concise code than anything else.

    Also you don't have to worry about indention as slavishly as in Fortran
which is what some people's experience with significant indention comes from.
 The following is perfectly legal and identical in Python:

if something or that_thing and not something_else:

if something or
   that_thing and
   not something_else:

    Also things like this:

foo = [spam,
       ham,
       eggs,
       baked beans,
       special sauce]

    Significant indention does not apply inside statements or declarations.
It really is quite natural if you indent properly in the first place.



Reply to: