arguments of a function/metaclass

R

rubbishemail

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
 
M

Michele Simionato

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
 
S

Stargaming

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
 
G

goodwolf

(e-mail address removed) 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/Cookbook/Python/Recipe/442418
 
G

Gabriel Genellina

goodwolf said:
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.
 
G

goodwolf

Gabriel Genellina je napisao/la:
goodwolf said:
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.

My error. Post was written directly. Sorry.
 
G

goodwolf

(e-mail address removed) 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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top