Instance variables vs. class variables in Python

Posted by deamon on Stack Overflow See other posts from Stack Overflow or by deamon
Published on 2010-04-26T15:14:29Z Indexed on 2010/04/26 15:23 UTC
Read the original article Hit count: 279

Filed under:
|
|
|
|

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 ariables:

MyController(Controller):

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

  def action(self, request):
    pass

© Stack Overflow or respective owner

Related posts about python

Related posts about variables