Re: Passing new fields to an object

2015-06-13 Thread Peter Otten
Steven D'Aprano wrote: > On Fri, 12 Jun 2015 16:53:08 +0100, Paulo da Silva wrote: > >> 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

Re: Passing new fields to an object

2015-06-12 Thread Paulo da Silva
On 13-06-2015 02:25, Steven D'Aprano wrote: > On Fri, 12 Jun 2015 16:53:08 +0100, Paulo da Silva wrote: > ... > > You should use SimpleNamespace, as Peter suggests, but *not* subclass it. > If you subclass it and add methods: > > class C(SimpleNamespace): > def foo(self, arg): > pr

Re: Passing new fields to an object

2015-06-12 Thread Paulo da Silva
On 12-06-2015 20:12, Peter Otten wrote: > Paulo da Silva wrote: > >> On 12-06-2015 17:17, Peter Otten wrote: >>> Paulo da Silva wrote: >>> >> ... ... > It *is* a class, and by making C a subclass of SimpleNamespace C inherits > the initialiser which does the actual work of updating the __dict__

Re: Passing new fields to an object

2015-06-12 Thread Steven D'Aprano
On Fri, 12 Jun 2015 16:53:08 +0100, Paulo da Silva wrote: > 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 parame

Re: Passing new fields to an object

2015-06-12 Thread Peter Otten
Paulo da Silva wrote: > On 12-06-2015 17:17, Peter Otten wrote: >> Paulo da Silva wrote: >> > ... > >> > import types > class C(types.SimpleNamespace): >> ... pass >> ... > c = C(f1=1, f2=None) > c >> C(f1=1, f2=None) >> > > Thanks for all your explanations. > This solutio

Re: Passing new fields to an object

2015-06-12 Thread Paulo da Silva
On 12-06-2015 17:17, Peter Otten wrote: > Paulo da Silva wrote: > ... > import types class C(types.SimpleNamespace): > ... pass > ... c = C(f1=1, f2=None) c > C(f1=1, f2=None) > Thanks for all your explanations. This solution works. Would you please detail a little on

Re: Passing new fields to an object

2015-06-12 Thread Paulo da Silva
On 12-06-2015 17:17, gst wrote: > Le vendredi 12 juin 2015 11:53:24 UTC-4, Paulo da Silva a écrit : > in the __init__, simply do: > > self.__dict__.update(**parms) > > regards, > Ok. Thanks. -- https://mail.python.org/mailman/listinfo/python-list

Re: Passing new fields to an object

2015-06-12 Thread gst
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

Re: Passing new fields to an object

2015-06-12 Thread Peter Otten
Paulo da Silva wrote: > 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? Use a dict

Passing new fields to an object

2015-06-12 Thread Paulo da Silva
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 -- https://mail.python