Can you create an instance of a subclass with an existing instance of the base class?

Sandra-24 sandravandale at yahoo.com
Sun Apr 23 10:14:02 EDT 2006


Lawrence D'Oliveiro wrote:
> In article <1145736124.831082.34550 at v46g2000cwv.googlegroups.com>,
>  "Sandra-24" <sandravandale at yahoo.com> wrote:
>
> >Now that is a clever little trick. I never would have guessed you can
> >assign to __class__, Python always surprises me in it's sheer
> >flexibility.
>
> That's because you're still thinking in OO terms.

It's not quite as simple as all that. I agree that people, escpecially
people with a Java (ew) background overuse OO, when there's often
simpler ways of doing things.

However in this case I'm simply getting an object (an mp_request object
from mod_python) passed into my function, and before I pass it on to
the functions that make up and individual web page it is modified by
adding members and methods to add functionality. It's not that I'm
thinking in OO, but that the object is a convienient place to put
things, especially functions that take an mp_request object as their
first argument.

Sadly I'm unable to create it as a python object first, because it's
created by the time my code comes into play. So I have to resort to
using the new module to add methods.

It works, but it has to be redone for every request, I thought moving
the extra functionality to another object would simplify the task. A
better way might be to contain the mp_request within another object and
use __getattr__ to lazily copy the inner object. I'd probably have to
first copy those few fields that are not read-only or use __setattr__
as well.

Thanks,
-Sandra




More information about the Python-list mailing list