Grouping code by indentation - feature or ******?

Patrik Andreasen pan at
Sun Mar 27 20:53:49 CEST 2005

Giovanni Bajo wrote:
> Terry Reedy wrote:

>>>3) Sometimes the structure of the algorithm is not the structure
>>>  of the code as written, people who prefer that the indentation
>>>  reflects the structure of the algorithm instead of the structure
>>>  of the code, are forced to indent wrongly.
>>Do you have any simple examples in mind?
> Yes. When I use PyQt (or similar toolkit), I would like to indent my widget
> creation code so that one indentiation level means one level down into the
> widget tree hierarchy:
> v = VBox(self)
>     # sons of v indented here
>     w = HBox(self)
>         # sons of w here
>         QLabel("hello", w)
>         QLabel("world", w)
>     QButton("ok", v)
> In fact, I am used to do this very thing in C++, and it helps readability a
> lot.

But in Python it's really easy to use a declarative construct instead, 
maybe something as simple as a nested list of classes and constructor 
arguments, and use some simple machinery to build the object graph. This 
way you avoid having the code and indentation saying different things, 
like in your example above, where it seems that w is not in fact a child 
of v. I don't know anything about PyQt though, so if I'm wrong about 
that I apologise - but the point still stands, since it's confusing to 
read it.


More information about the Python-list mailing list