Re: idiom for constructor?

2005-06-06 Thread tracyshaun
And you probably should add: ... def __init__(self, *args): assert len(args) == len(self.__slots__) ... --T -- http://mail.python.org/mailman/listinfo/python-list

Re: idiom for constructor?

2005-06-06 Thread tracyshaun
How about just doing this: class Foo(object): __slots__ = ('a','b','c','d') def __init__(self, *args): for (name, arg) in zip(self.__slots__, args): setattr(self, name, arg) --T -- http://mail.python.org/mailman/listinfo/python-list

Re: idiom for constructor?

2005-06-04 Thread Steven Bethard
Peter Dembinski wrote: > From the other side: what are the usual uses of 'exec'? I have to say, I have yet to find a use for it. My intuition is that the good use cases for 'exec' have something to do with writing programs that allow users to interactively script the program actions. But I've

Re: idiom for constructor?

2005-06-04 Thread Volker Grabsch
Peter Dembinski wrote: >> This is not a good use case for exec. Use setattr: > > OK, true. > From the other side: what are the usual uses of 'exec'? An interactive Python interpreter. :-) No, seriously: Introspection is always better than exec. It is far less error phrone, especially because yo

Re: idiom for constructor?

2005-06-04 Thread Peter Dembinski
Steven Bethard <[EMAIL PROTECTED]> writes: > Peter Dembinski wrote: >>class A: >>def __init__(self, a, b, c, d): >>initial = {'a' : a, 'b' : b, 'c' : c, 'd' : d} >>for param in initial.keys(): >>exec "self.%s = initial['%s']" % (param, param) > > This is not a good

Re: idiom for constructor?

2005-06-04 Thread Steven Bethard
Peter Dembinski wrote: >class A: >def __init__(self, a, b, c, d): >initial = {'a' : a, 'b' : b, 'c' : c, 'd' : d} >for param in initial.keys(): >exec "self.%s = initial['%s']" % (param, param) This is not a good use case for exec. Use setattr: for param in initial

Re: idiom for constructor?

2005-06-04 Thread Peter Dembinski
Peter Dembinski <[EMAIL PROTECTED]> writes: [snap] Eh, sorry, it should look like this: > #v+ > > class A: > def __init__(self, a, b, c, d): > initial = {'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4} initial = {'a' : a, 'b' : b, 'c' : c, 'd' : d} > > for param in in

Re: idiom for constructor?

2005-06-04 Thread Peter Dembinski
Steven Bethard <[EMAIL PROTECTED]> writes: > Mac wrote: >> Is there a nice Python idiom for constructors which would expedite >> the following? >> class Foo: >> def __init__(self, a,b,c,d,...): >> self.a = a >> self.b = b >> self.c = c >> self.d = d >> ... > > py> class Foo(o

Re: idiom for constructor?

2005-06-01 Thread Mac
This was the direction I was aiming for initially, and I have used this form before, but was hoping there was a way I could go one step further, and somehow get rid of the repetition of "self."... Heh, ideally: self.{a,b,c,d} = a,b,c,d Alas, not Python syntax... :) I do like Steven's form of

Re: idiom for constructor?

2005-06-01 Thread Terry Reedy
"Mac" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Is there a nice Python idiom for constructors which would expedite the > following? > > class Foo: > def __init__(self, a,b,c,d,...): >self.a = a >self.b = b >self.c = c >self.d = d Do you like this better? sel

Re: idiom for constructor?

2005-06-01 Thread Terry Hancock
On Wednesday 01 June 2005 12:50 pm, Mac wrote: > Is there a nice Python idiom for constructors which would expedite the > following? > > class Foo: > def __init__(self, a,b,c,d,...): > self.a = a > self.b = b > self.c = c > self.d = d > ... > > I would like to keep the __ini

Re: idiom for constructor?

2005-06-01 Thread Steven Bethard
Mac wrote: > Is there a nice Python idiom for constructors which would expedite the > following? > > class Foo: > def __init__(self, a,b,c,d,...): > self.a = a > self.b = b > self.c = c > self.d = d > ... py> class Foo(object): ... def __init__(self, a, b, c, d): ...

Re: idiom for constructor?

2005-06-01 Thread Laszlo Zsolt Nagy
>You could try: > >class Foo: > def __init__(self,a,b,c,d): > args = locals() > for arg in args.keys(): > if name!='self': > self.__dict__[arg] = args[arg] > > Should be: if arg!='self' Also it is not perfect. For example: class Foo: def __init__(self,

Re: idiom for constructor?

2005-06-01 Thread Mac
> You might look to see if you can customize your editor to use > templates/interaction and then inserts the right text for you. Well, I'm not really concerned with "amount of typing" as much as with the inherent ugliness, tediousness, and lack of elegance of the original form. Alas, templates/mac

Re: idiom for constructor?

2005-06-01 Thread Laszlo Zsolt Nagy
Mac wrote: >Is there a nice Python idiom for constructors which would expedite the >following? > >class Foo: > def __init__(self, a,b,c,d,...): >self.a = a >self.b = b >self.c = c >self.d = d >... > >I would like to keep the __init__ parameter list explicit, as is, >rather tha

Re: idiom for constructor?

2005-06-01 Thread Chris Green
"Mac" <[EMAIL PROTECTED]> writes: > Is there a nice Python idiom for constructors which would expedite the > following? > > class Foo: > def __init__(self, a,b,c,d,...): > self.a = a > ... You could try: class Foo: def __init__(self,a,b,c,d): args = locals() for arg in

idiom for constructor?

2005-06-01 Thread Mac
Is there a nice Python idiom for constructors which would expedite the following? class Foo: def __init__(self, a,b,c,d,...): self.a = a self.b = b self.c = c self.d = d ... I would like to keep the __init__ parameter list explicit, as is, rather than passing in a dictionary