views:

308

answers:

3

I have Python classes, of which I need only one instance at runtime, so it would be sufficient to have the attributes only once per class and not per instance. If there would be more than one instance (what won't happen), all instance should have the same configuration. I wonder which of the following options would be better or more "idiomatic" Python.

Class variables:

MyController(Controller):

  path = "something/"
  childs = [AController, BController]

  def action(request):
    pass

Instance variables:

MyController(Controller):

  def __init__(self):
    self.path = "something/"
    self.childs = [AController, BController]

  def action(self, request):
    pass
+8  A: 

If you have only one instance anyway, it's best to make all variables per-instance, simply because they will be accessed (a little bit) faster (one less level of "lookup" due to the "inheritance" from class to instance), and there are no downsides to weigh against this small advantage.

Alex Martelli
Never heard of the Borg pattern? Only having one instance was the wrong way to have it in the first place.
Devin Jeanpierre
@Devin, yep, I've heard of the Borg pattern, since I'm the one who introduced it (in 2001, cfr http://code.activestate.com/recipes/66531-singleton-we-dont-need-no-stinkin-singleton-the-bo/ ;-). But there's nothing wrong, in simple cases, with simply having a single instance with no enforcement.
Alex Martelli
+5  A: 

When in doubt, you probably want an instance attribute.

Class attributes are best reserved for special cases where they make sense. The only very-common use case is methods. It isn't uncommon to use class attributes for read-only constants that instances need to know (though the only benefit to this is if you also want access from outside the class), but you should certainly be cautious about storing any state in them, which is seldom what you want. Even if you will only have one instance, you should write the class like you would any other, which usually means using instance attributes.

Mike Graham
The class variables are a kind of read-only constants. If Python let me define constants, I would have written it as constants.
deamon
@deamon, I am slightly more likely to put my constants completely outside of a class definitions and name them in all caps. Putting them inside the class is fine too. Making them instance attributes won't hurt anything, but might be a bit odd. I don't think this is an issue where the community gets behind one of the options too much.
Mike Graham
+2  A: 

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:

  1. locals
  2. nonlocals
  3. globals
  4. built-ins

for attribute access, the order is:

  1. instance
  2. class
  3. 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).

wescpy
@wescpy, but `MyController` is looked up in the globals, so the total cost is higher than `self.path` where `path` is an instance variable (since `self` is _local_ to the method == super-fast lookup).
Alex Martelli
ah, true. good catch. i guess the only workaround is to create a local reference... at this point, it's not really worth it.
wescpy