Tabs versus Spaces in Source Code ('semantic' vs. arbitrary indentation)

Dave Hansen iddw at hotmail.com
Wed May 17 21:17:04 CEST 2006


On Wed, 17 May 2006 12:02:46 -0700 in comp.lang.python, "Carl J. Van
Arsdall" <cvanarsdall at mvista.com> wrote:

>Andy Sy wrote:
>> Carl J. Van Arsdall wrote:
>>
>>   
>>>> Next major objection then, how can one practically use 'tabs as
>>>> semantic indentation' without screwing up formatting of code like
>>>> the below??
>>>>
>>>>
>>>>   def sqlcall():
>>>>       cursor.execute('select id, item, amount, field4, field5, field6'+
>>>>                      'from table1 where amount>100')
>>>>
>>>>   
>>>>       
>>> Why couldn't you tab your third line over as close as possible to the 
>>> start of the quote then use a couple spaces?  Then the tabs would work 
>>> just fine and you could still have your pretty line.
>>>     
>>
>>
>> This will break if someone tries to view my code using different
>> tab settings from the one I'm using
>>   
>Uh, no it won't.  It should be read in as "one tab and two spaces" 
>regardless of the tabstop setting.

Think about it a little harder.  What does that "one tab" mean?

Assume the code was written by someone using 4-space tabs.  To them,
the code is:

   def sqlcall():
   --->cursor.execute('select id, item, amount, field4, <etc>
   --->--->--->--->...'from table1 where amount>100')

(where -------> represents an 4-space tab and . represents a space)

Which looks fine.  But if I then load the code into my editor with
3-space tabs, it looks like:

   def sqlcall():
   -->cursor.execute('select id, item, amount, field4, <etc>
   -->-->-->-->...'from table1 where amount>100')

Then my colleage loads it into his editor with 8-space tabs, and it
looks like (assuming I didn't change it to make it look reasonable):

   def sqlcall():
   ------->cursor.execute('select id, item, amount, field4, <etc>
   ------->------->------->------->...'from table1 where amount>100')

In each case the second line has four TABs and three spaces.  But only
with the original TAB settings does it line up properly.

The only solution (as posted elsewhere) is to somehow enforce tabs for
indentation and spaces for spacing:

   def sqlcall():
   --->cursor.execute('select id, item, amount, field4, <etc>
   --->...............'from table1 where amount>100')

However, to twist an observation I've read about C++, while it's
clearly possible to use TABs in a sensible manner like this, it seems
that no one does.  Or at least, it doesn't last much beyond the
original author of the code...

Regards,
                                        -=Dave

-- 
Change is inevitable, progress is not.



More information about the Python-list mailing list