[help]How to load a class dynamic?

Alex Martelli aleax at aleax.it
Wed May 14 04:25:38 EDT 2003


<posted & mailed>

dolephi9080 wrote:


> Yes, you are right my info is not detail.  I think the first step I
> should make the instance. 

I think I'm not following what you're saying.  To make an instance
you need to call the class with the appropriate arguments; how can
that be the FIRST step?  First you need the class object -- you must
have that *before* you call it, no?

What I was saying is that if you have NO information about that the
'appropriate arguments' are, to be passed when calling the class
object, you're stuck.  If you DO know, then you're OK.

> and than assume the manifest of the 3 party
> provide another method and the argument of that method.
> it's look like this
> class = "MyPackage.MyModule.MyClass"
> method = "performAction"
> argument = "some value"
> 
> After I make the instance from the class, I think I can use
> introspection to call the method and argments. That's my hypothesis. I
> am not sure it can work or not. So what I can do is try to solve the
> first problem and than  go to the second step.

And I'm focusing on the 'first problem' and telling you that, according
to the specs you've given and are now repeating, it cannot be solved,
as you keep implying that you lack ANY information about how the class
is to be called in order to instantiate it.  If that's true -- if you
have NO idea whatsoever of what arguments the class needs in order to
properly instantiate itself -- then you just can't instantiate it, no?


> and what your mean is when the constructor without any argument the way
> you are telling can work very well. if the constructor need argument
> will broken the way, right?

Sorry, I do not understand what you mean by "will broken the way".  If
you call any callable without arguments when that callable demands some
argument, you will raise a TypeError exception.  You can catch it with
an except clause (as long as you perform the call in the corresponding
try clause of a try/except statement), but HOW do you handle it IF you
have no information about arguments you actually need to pass?


Alex





More information about the Python-list mailing list