Inheriting automatic attributes initializer considered harmful?

Thomas Wittek mail at gedankenkonstrukt.de
Wed Oct 17 07:23:49 EDT 2007


Hi!

I'm relatively new to Python, so maybe there is an obvious answer to my
question, that I just didn't find, yet.

I've got quite some classes (from a data model mapped with SQL-Alchemy)
that can be instatiated using kwargs for the attribute values. Example:

  class User(object):
      def __init__(self, name=None):
          self.name = name

  u = User(name="user name")

Writing such constructors for all classes is very tedious.
So I subclass them from this base class to avoid writing these constructors:

  class AutoInitAttributes(object):
      def __init__(self, **kwargs):
          for k, v in kwargs.items():
              getattr(self, k) # assure that the attribute exits
              setattr(self, k, v)

Is there already a standard lib class doing (something like) this?
Or is it even harmful to do this?
Although I cannot see any problems with it, I feel very unsafe about
that, because I've not seen this (in the few lines from some tutorials)
before.

Regards
-- 
Thomas Wittek
Web: http://gedankenkonstrukt.de/
Jabber: streawkceur at jabber.i-pobox.net
GPG: 0xF534E231



More information about the Python-list mailing list