[Tutor] Class instantiation parameters

Kent Johnson kent37 at tds.net
Sun Aug 7 14:40:40 CEST 2005


Jan Eden wrote:
> In Python, I would do:
> 
> page = Show(type=type, id=id)
> 
> class Show: def __init__(self, type, id): self.id = id self.type =
> type ... return self
> 
> For two parameters, this is relatively simple. But if I have for
> example 10 parameters on instantiation, assigning each value the the
> class object manually will be really clumsy.
> 
> So how can I add the values of all the paramaters to my class
> instance in one step?

There was recently a long discussion of this on comp.lang.python.
http://groups.google.com/group/comp.lang.python/browse_frm/thread/7346ad00a14e821a/9dc993d295475cac?q=locals()&rnum=15&hl=en#9dc993d295475cac

A simple solution is
    def __init__ (self, x, y, z):
        self.__dict__.update(locals())
        del self.self

Kent



More information about the Tutor mailing list