further echoing mike's and alex's advice and adding my own color...
using instance attributes are the typical, more idiomatic Python. class attributes are not oft-used -- at least not in production code in my last 13+ consecutive years of Python. the same is true for static and class methods... just not very common unless there's a specific use case or an aberrant programmer wanting to show off they know some obscure corner of Python programming.
alex mentions in his reply that access will be (a little bit) faster due to one less level of lookup... let me further clarify for those who don't know about how this works yet, it is very similar to variable access -- the search happens in this order:
- locals
- nonlocals
- globals
- built-ins
for attribute access, the order is:
- instance
- class
- base classes as determined by the MRO (method resolution order)
in your example above, let's say you're looking up the path
attribute. when it encounters a reference like "self.path
", Python will look at the instance attributes first for a match; when that fails, it checks the class from which the object was instantiated from. finally, it will search the base classes. as alex has stated, if your attribute is found in the instance, it won't defer to the class, hence your little bit of time savings.
however, if you insist on class attributes, you will have to give up this tiny bit of performance, or, your other alternative is to refer to the object via the class instead of the instance, e.g., MyController.path
instead of self.path
. that's a direct lookup which will get around the deferred lookup, but as alex mentions below, that's a global variable, so you lose that bit that you thought you were going to save (unless you create a local reference to the [global] class name).