Python: How To copy function parameters into object's fields effortlessly ?

Posted by bandana on Stack Overflow See other posts from Stack Overflow or by bandana
Published on 2010-05-26T11:54:15Z Indexed on 2010/05/26 12:11 UTC
Read the original article Hit count: 215

Filed under:
|

Many times I have member functions that copy parameters into object's fields. For Example:

class NouveauRiches(object):
  def __init__(self, car, mansion, jet, bling):
    self.car = car
    self.mansion = mansion
    self.jet = jet
    self.bling = bling

Is there a python language construct that would make the above code less tedious? One could use *args:

def __init__(self, *args):
  self.car, self.mansion, self.jet, self.bling = args

+: less tedious

-: function signature not revealing enough. need to dive into function code to know how to use function

-: does not raise a TypeError on call with wrong # of parameters (but does raise a ValueError)

Any other ideas? (Whatever your suggestion, make sure the code calling the function does stays simple)

© Stack Overflow or respective owner

Related posts about python

Related posts about parameters