First day beginner to python, add to counter after nested loop

rurpy at yahoo.com rurpy at yahoo.com
Wed Oct 30 13:02:33 EDT 2013


On 10/30/2013 08:22 AM, Alister wrote:
> On Wed, 30 Oct 2013 13:42:37 +0100, Antoon Pardon wrote:
>> Op 30-10-13 13:17, Chris Angelico schreef:
>>> On Wed, Oct 30, 2013 at 11:01 PM, Antoon Pardon
>>> <antoon.pardon at rece.vub.ac.be> wrote:
>>> I broadly agree with your post (I'm of the school of thought that
>>> braces are better than indentation for delimiting blocks), but I don't
>>> think this argument holds water. All you need to do is be consistent
>>> about tabs OR spaces (and I'd recommend tabs, since they're simpler and
>>> safer), and you'll never have this trouble.
>> 
>> Easier said than done. First of all I can be as consistent as possible,
>> I can't just take code from someone else and insert it because that
>> other person may be consistenly doing it different from me.
> 
> I disagree it is very easy.
>[...]
> 2) when importing code from someone else a simple search & replace of tab 
> with 4 spaces will instantly correct the formatting on code using tab 
> without breaking code that doesn't.

Tabs can occur in strings as well as tokens in Python 
code and such code will be broken by a global search and
replace:

    print ("      %s" % message)
            ^^^^^^--- This is a literal tab.

Or:

    CSVDATA = '''
    item    qty	    cost
    44522   100     30.25
    44107   55      15.50
    45229   1007    77.20
    '''
where the spaces between the columns above need to be
tabs to be correctly processed as csv text.

Your "easy fix" will break code like the above.
(And please note that because you can write the above 
using "\t" or avoid literal tabs in other ways, that:
1) Does not negate the fact that the above code is
 legal python code that is broken by your suggestion.
2) Alternatives such as using '\t' have their own 
 downsides such as readability or lack of compatibility 
 with other parts of a larger system.

Note too that even replacing only leading tabs is not 
safe since the cvs data above could have leading tabs.



More information about the Python-list mailing list