And you probably should add:
...
def __init__(self, *args):
assert len(args) == len(self.__slots__)
...
--T
--
http://mail.python.org/mailman/listinfo/python-list
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
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
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
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
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
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
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
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
"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
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
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):
...
>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,
> 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
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
"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
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
17 matches
Mail list logo