using __init__ with params ( somewhat long )
Johann Hibschman
johann at physics.berkeley.edu
Tue Jan 16 14:38:39 EST 2001
Corey writes:
> def __init__(self, dictionary, attr1=None, attr2=None, attr3=None):
> Is this considered overkill? It seems like it to me when thinking
> about it, but I'm perhaps too stuck in the Perl "There's More Than
> One Way To Do It" mindset... and so want to make this class as easy
> to use as possible within the context/methodology of which it gets
> utilized.
I often write code like:
class MyClass:
def __init__(self, attr1=None, attr2=None, attr3=None):
self.attr1 = attr1 # I interpret "None" as "uninitialized".
self.attr2 = attr2
self.attr3 = attr3
If you absolutely must pass in a dictionary, you can use
instance = apply(MyClass, (), {'attr1': 1, 'attr2': 2, 'attr3': 3})
using the third argument to apply.
If the case of keywords is wrong, an TypeError will be raised.
Is this good enough?
--
Johann Hibschman johann at physics.berkeley.edu
More information about the Python-list
mailing list