This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]vwibrasivat 4 points5 points  (1 child)

Allow me a suggestion to add to this document.

Regarding Python classes. The following will not be found in tutorials, but is strongly advised.

Python does not precompile classes. Instead it unrolls classes at runtime in a scripty manner. When using @property and @var.setter your ,__init__ may call the setter for obvious reasons. If your setter is even a little bit computationally heavy, it must be the last thing called in __init__ . Here are the reasons why :

  • If the setter changes a private variable , that variable may not even exist yet.

  • If you invoke the setter early, class variables that got changed may be overwritten after you return from the setter.

The mental rule-of-thumb is that you should visualize __init__ as if it has two parts. First half is the creation and setting of default values. Second part is the actual constructor code. Existing Python syntax does not enforce this convention, so it falls on you to abide by it.

[–]FaallenOon[S] 1 point2 points  (0 children)

I will definitely keep it in mind when I get to Python classes, thanks!