Calling a constructor by reference

Edwin Young edwin at bathysphere.org
Thu Oct 7 09:53:06 EDT 2004


Hi,

I know there must be a way to do this but the correct syntax eludes me.

I want to write a method which I can pass different types to and have
it construct them.

I tried this:

>>>
class X:
    def __init__(self):
        pass

def make_thing(type,*args):
    return type(args)

x = X()
y = make_thing(X)

print x, y 
>>>

But get 'TypeError: __init__() takes exactly 1 argument (2
given)'. Presumably X is treated as an unbound instance method?

Also, the types I want to create inherit from an old-style class in a
package, so I don't think I can make X a new-style class (if that's
relevant).

How should I go about this?

Thanks,
--
Edwin





More information about the Python-list mailing list