469,955 Members | 1,402 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,955 developers. It's quick & easy.

create instance attributes for every method argument

I remember reading somewhere how to create an instance attribute for
every method argument, but although Google is my friend, I can't seem
to find it. This could likely be done way more elegant:

=========================
class Test(object):

def __init__(self, a, b, c, d, e, f):
self.a = a
self.b = b
self.c = c
self.d = d
=========================

2B
Jul 19 '08 #1
3 815
Berco Beute wrote:
I remember reading somewhere how to create an instance attribute for
every method argument, but although Google is my friend, I can't seem
to find it. This could likely be done way more elegant:

=========================
class Test(object):

def __init__(self, a, b, c, d, e, f):
self.a = a
self.b = b
self.c = c
self.d = d
=========================

2B
IMHO you can't do much better than that with positional arguments, but you can
if they are keyword arguments.

class foo(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)

-Larry

Jul 19 '08 #2
Berco Beute <cy*****@gmail.comwrote:
I remember reading somewhere how to create an instance attribute for
every method argument, but although Google is my friend, I can't seem
to find it. This could likely be done way more elegant:

=========================
class Test(object):

def __init__(self, a, b, c, d, e, f):
self.a = a
self.b = b
self.c = c
self.d = d
=========================

2B
You *could* do something like this:
>>class Test(object):
def __init__(self, a, b, c, d, e, f):
self.update(locals())

def update(self, adict):
for k in adict:
if k != 'self':
setattr(self, k, adict[k])

>>c = Test(1, 2, 3, 4, 5, 6)
c.__dict__
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'f': 6}
>>>
but to be honest, your original is much clearer as it expresses the
intention without any obfuscation and as soon as you want to do anything
more than simply copying all arguments you'll want to do the assignments
individually anyway.
Jul 19 '08 #3
Berco Beute wrote:
I remember reading somewhere how to create an instance attribute for
every method argument, but although Google is my friend, I can't seem
to find it. This could likely be done way more elegant:

=========================
class Test(object):

def __init__(self, a, b, c, d, e, f):
self.a = a
self.b = b
self.c = c
self.d = d
=========================
http://code.activestate.com/recipes/280381/

Personally, I prefer to spell it out like you did above.

Peter
Jul 19 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

26 posts views Thread by Fernando M. | last post: by
39 posts views Thread by Quick Fox | last post: by
10 posts views Thread by John M. Gabriele | last post: by
4 posts views Thread by kplkumar | last post: by
6 posts views Thread by Rob Cowie | last post: by
3 posts views Thread by Joseph Lu | last post: by
2 posts views Thread by Jake K | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.