Creating dynamic objects with dynamic constructor args

Furkan Kuru furkankuru at gmail.com
Tue Mar 25 12:08:07 EDT 2008


you can call a function with arglist like this:

def dummy(a, b, c):
    print a + b, c

args = [1, 2, "hello"]
dummy(*args)

On Tue, Mar 25, 2008 at 5:38 PM, <Greg at bag.python.org> wrote:

> I'd like to create objects on the fly from a pointer to the class
> using:  instance = klass()  But I need to be able to pass in variables
> to the __init__ method.  I can recover the arguments using the
> inspect.argspec, but how do I call __init__ with a list of arguments
> and have them unpacked to the argument list rather than passed as a
> single object?
>
> ie. class T:
>        def __init__(self, foo, bar):
>                self.foo = foo
>                self.bar = bar
>
> argspec = inspect.argspec(T.__init__)
> args = (1, 2)
>
> ??? how do you call T(args)?
>
> Thanks.
> Greg
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Furkan Kuru
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080325/656cf9df/attachment-0001.html>


More information about the Python-list mailing list