Messing around with something beautiful

June 16th, 2005 by Patrick Boucher - Viewed 7267 times -




The Python language has rather strict indentation rules. These rules exist because the indentation is actually part of the language syntax and is used to define blocks of code. But sometimes, maybe sometimes, you’d like to be able to indent things the way you like. An example of this is PPG layout code in XSI when said layout includes tabs and/or groups.

For example:

1
2
3
4
5
6
7
oLayout.AddGroup("Group1", True)
oLayout.AddItem("Item1", "Item1")
oLayout.AddEnumControl("Item2", myList, "Item2", c.siControlRadio)
oLayout.AddGroup("Example", True)
oLayout.AddStaticText("New Label")
oLayout.EndGroup()
oLayout.EndGroup()

Might be a bit easier to understand like so:

1
2
3
4
5
6
7
oLayout.AddGroup("Group1", True);\
    oLayout.AddItem("Item1", "Item1");\
    oLayout.AddEnumControl("Item2", myList, "Item2", c.siControlRadio);\
    oLayout.AddGroup("Example", True);\
        oLayout.AddStaticText("New Label");\
    oLayout.EndGroup();\
oLayout.EndGroup()

You can put a ; at the end of your Python statement but because it is optional, why would you? Well, combined with the \ character that allows you to wrap long lines, you can trick Python into thinking that you have many statements on a single line and thus arbitrarily indent them.

Please, use this sparingly! The Python language was designed in such a way that makes it a very legible language. Using this trick for the wrong reasons can quickly make it an unintelligible tangle.

Have fun!

2 Responses to “Messing around with something beautiful”

  1. I really admire python users so much, I”ve tried so many times to use this language, but the syntax is so different from all the languages I”ve used since now.
    I didnít know python allows to close lines with semi-columns, now Iím just missing the most important part for me to read the code, brackets!!! have you got any trick about this? ; )

    Thanks Patrick for sharing!

    Andrea Interguglielmi
    —————————————-
    Technical Artist
    Lionhead Studios
    Central Technology Group\ Art
    —————————————-

  2. [...] s code that creates the same menu you saw above. Please note that this code uses one of my stupid python tricks. def callback(data): Application.Lo [...]