By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

arguments of a function/metaclass

P: n/a
Hello,
I have a member function with many (20) named arguments

def __init__(self,a=1,b=2):
self.a=a
self.b=b

I would like to get rid of the many redundant lines like self.a=a and
set the members automatically.
The list of default arguments could be given like

def __init__(**kwargs):
arglist={"a":1,"b":2]

if this makes things easier

Of course there has to be a check that raises an error in case of an
unknown argument not mentioned in this list.
I am sure there is an elegant way how to do this, could you give me any
hints???
Many thanks

Daniel

Jan 16 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
ru**********@web.de wrote:
Hello,
I have a member function with many (20) named arguments

def __init__(self,a=1,b=2):
self.a=a
self.b=b

I would like to get rid of the many redundant lines like self.a=a and
set the members automatically.
The list of default arguments could be given like

def __init__(**kwargs):
arglist={"a":1,"b":2]

if this makes things easier

Of course there has to be a check that raises an error in case of an
unknown argument not mentioned in this list.
I am sure there is an elegant way how to do this, could you give me any
hints???
def __init__(self, **kw):
vars(self).update(kw)
Michele Simionato

Jan 16 '07 #2

P: n/a
ru**********@web.de schrieb:
Hello,
I have a member function with many (20) named arguments

def __init__(self,a=1,b=2):
self.a=a
self.b=b

I would like to get rid of the many redundant lines like self.a=a and
set the members automatically.
The list of default arguments could be given like

def __init__(**kwargs):
arglist={"a":1,"b":2]

if this makes things easier

Of course there has to be a check that raises an error in case of an
unknown argument not mentioned in this list.
I am sure there is an elegant way how to do this, could you give me any
hints???
Many thanks

Daniel
If it's a long list of arguments, it will stay a long list (i. e.
representation) of arguments, whatever you do. You could minimize your
layout, though, to e. g. use a decorator that takes a list of arguments
automatically saved to self.
But that's just a "layout" (or design) issue and it will stay clumsy,
whatever you do (perhaps splitting up the dict to many lines will make
it more readable, but that's it).

To bring up a more liberate attempt, why don't you just save *all* args
received (self.__dict__.update(kwargs)). If the user provides more
arguments -- nevermind!
You would have to do something about default values though and here you
got to use the list again, first updating self.__dict__ with the list
and afterwards with kwargs.

Stargaming
Jan 16 '07 #3

P: n/a

ru**********@web.de je napisao/la:
Hello,
I have a member function with many (20) named arguments

def __init__(self,a=1,b=2):
self.a=a
self.b=b

I would like to get rid of the many redundant lines like self.a=a and
set the members automatically.
The list of default arguments could be given like

def __init__(**kwargs):
arglist={"a":1,"b":2]

if this makes things easier

Of course there has to be a check that raises an error in case of an
unknown argument not mentioned in this list.
I am sure there is an elegant way how to do this, could you give me any
hints???
Many thanks

Daniel
A simply solution:

def __init__(self, a=1, b=2, c=3, ......):
for key, val in locals().items():
if key != 'self':
setattr(self.__class__, key, val)

in addition:

def set(self, **kwarg):
for key in kwargs:
if hasattr(self.__class__, key):
setattr(self.__class__, key, kwargs[key])
else:
raise ....

This solution is appropriate with use of proprieties.
For better proprety usage look at
http://aspn.activestate.com/ASPN/Coo.../Recipe/442418

Jan 16 '07 #4

P: n/a
"goodwolf" <Ro**********@gmail.comescribió en el mensaje
news:11**********************@s34g2000cwa.googlegr oups.com...
A simply solution:

def __init__(self, a=1, b=2, c=3, ......):
for key, val in locals().items():
if key != 'self':
setattr(self.__class__, key, val)

in addition:

def set(self, **kwarg):
for key in kwargs:
if hasattr(self.__class__, key):
setattr(self.__class__, key, kwargs[key])
else:
raise ....
Why setattr(self.__class__,...) instead of setattr(self, ...)? You're
modifying the class attributes, and that's not usually intended.

--
Gabriel Genellina
Jan 16 '07 #5

P: n/a

Gabriel Genellina je napisao/la:
"goodwolf" <Ro**********@gmail.comescribió en el mensaje
news:11**********************@s34g2000cwa.googlegr oups.com...
A simply solution:

def __init__(self, a=1, b=2, c=3, ......):
for key, val in locals().items():
if key != 'self':
setattr(self.__class__, key, val)

in addition:

def set(self, **kwarg):
for key in kwargs:
if hasattr(self.__class__, key):
setattr(self.__class__, key, kwargs[key])
else:
raise ....

Why setattr(self.__class__,...) instead of setattr(self, ...)? You're
modifying the class attributes, and that's not usually intended.

--
Gabriel Genellina
My error. Post was written directly. Sorry.

Jan 16 '07 #6

P: n/a

ru**********@web.de je napisao/la:
Hello,
I have a member function with many (20) named arguments

def __init__(self,a=1,b=2):
self.a=a
self.b=b

I would like to get rid of the many redundant lines like self.a=a and
set the members automatically.
The list of default arguments could be given like

def __init__(**kwargs):
arglist={"a":1,"b":2]

if this makes things easier

Of course there has to be a check that raises an error in case of an
unknown argument not mentioned in this list.
I am sure there is an elegant way how to do this, could you give me any
hints???
Many thanks

Daniel
def changeattrs(obj, __dict, **kwargs):
for name, value in __dict.iteritems():
if hasattr(obj, name):
setattr(obj, name, value)
else:
raise AttributeError
for name in kwargs:
if hasattr(obj, name):
setattr(obj, name, kwargs[name])
else:
raise AttributeError
def locals2self(depth=0):
import sys
ns = sys._getframe(depth+1).f_locals
obj = ns['self']
for name, value in ns.iteritems():
if name != 'self':
setattr(obj, name, value)

class C1(object):
def __init__(self, a=1, b=2, c=3, d=4):
locals2self()

class C2(object):
a = 1
b = 2
c = 3
d = 4

def __init__(self, **kwargs):
changeattrs(self, kwargs)

Jan 16 '07 #7

P: n/a

thank you!

Jan 18 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.