[Tutor] Scope and elegance revisited

James Newton jnewton at fuelindustries.com
Wed Jan 9 21:12:27 CET 2008


Hi Python Purists!

I want all instances of a given class to share a piece of information,
but I want to set that information on the fly.  I have found that this
works:


  class Foo(object):
      # class_variable = None # There is no real need to declare this 

      def __init__(self):
          print self.__class__.class_variable


  def main():
      Foo.class_variable = "Done"
      Foo()
    

  if __name__ == '__main__': main()


Running the script prints out "Done" as expected.

However, this looks ugly to me.  Is there a more elegant way of doing
this?


To give you the context: my application allows you to select a skin for
the user interface.  I want to set the access path to the skin folder as
a class variable, so that all instances of that class use images from
the appropriate folder.  The access path will be read in from a
preferences file before any instances of the class are created.

Thanks in advance for your advice.

James


More information about the Tutor mailing list