Calling a constructor by reference
Russell Blau
russblau at hotmail.com
Thu Oct 7 09:51:31 EDT 2004
"Edwin Young" <edwin at bathysphere.org> wrote in message
news:m37jq2fxdp.fsf at localhost.localdomain...
>
> 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?
Hint: what do you see if you run the following function?
>>> def show_args(type, *args):
print args
>>> show_args()
You just need to change your constructor [__init__()] to accept the correct
number of arguments.
--
I don't actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.
More information about the Python-list
mailing list