allen.fowler schrieb:
Hello,

I've got a bunch of code that looks something like:

class MyOb(object):
  def __init__(self, p1=None, p2=None, p3=None, ...):
    self.p1 = p1
    self.p2 = p2
    self.p3 = p3
    self.pN = ...


ob1 = MyOb(p1="Tom", p3="New York")
ob2 = MyOb(p1="Joe", p2="j...@host", p3="New Jersey")

... and so on.

This is fine for only a few parameters, but it's very ugly and a lot
of duplicate typing once I've got 10+ parameters and 5 kinds of
objects.

Is there a better way to do this?

There are some tricks. Like this


def __init__(self, p1=None, ...):
    d = locals()
    del d["self"]
    self.__dict__.update(d)


However, it looks like a code-smell for me if you have 10+ paramters.

Diez
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to