using __init__ with params ( somewhat long )

Neil Schemenauer nas at arctrix.com
Tue Jan 16 08:09:19 EST 2001


On Tue, Jan 16, 2001 at 07:19:21PM +0000, Corey wrote:
> I'm not quite sure how to go about defining a "proper" Python class 
> constructor, where the user can optionaly pass a parameter, or multiple 
> parameters at instantiation, as a shortcut to initializing the object's
> attributes in one pass, rather than using multiple 'set' methods after
> instantiation.

Here is one (untested) solution:

    def MyClass:
        def __init__(self, **kwargs):
            for name, value in kwargs.items():
                setattr(self, name, value)
            
Cheers,

  Neil




More information about the Python-list mailing list