Code redundancy
Ethan Furman
ethan at stoneleaf.us
Tue Apr 20 18:48:57 EDT 2010
Alan Harris-Reid wrote:
> The code is not usually in class.__init__ (otherwise I would have used
> the self. prefix), but I like your self.__dict__.update(...) solution
> and I'll try and remember it.
>
> The code I was thinking of goes something like as follows (don't have a
> specific example to hand, but the principal is the same)...
>
> NewClass = BaseClass()
> NewClass.attr1 = value1
> NewClass.attr2 = value2
> NewClass.attr3 = value3
> etc.
>
> So if there are more than a couple of attributes to set for a class
> instance, how would you approach it (short of passing the values as
> parameters to BaseClass)?
Unless I'm missing something (your use-case, perhaps? ;) in this example
NewClass is *not* a class -- it's an instance of BaseClass, and you are
dynamically adding attributes to it.
It's definitely a switch coming from FoxPro (me, too!), but it is well
worth it once your brain starts working pythonically.
~Ethan~
More information about the Python-list
mailing list