Passing new fields to an object

gst g.starck at gmail.com
Fri Jun 12 12:17:00 EDT 2015


Le vendredi 12 juin 2015 11:53:24 UTC-4, Paulo da Silva a écrit :
> I would like to do something like this:
> 
> class C:
> 	def __init__(self,**parms):
> 	...
> 
> c=C(f1=1,f2=None)
> 
> I want to have, for the object
> 	self.f1=1
> 	self.f2=None
> 
> for an arbitrary number of parameters.
> 
> What is the best way to achieve this?
> 
> Thanks

in the __init__, simply do:

    self.__dict__.update(**parms)

regards,

gst.



More information about the Python-list mailing list