Question about instance creation

Joshua Macy amused at webamused.com
Mon Mar 20 18:31:01 EST 2000


Robin Barendregt wrote:
> 
> "Gordon McMillan" <gmcm at hypernet.com> wrote:
> 
> >So, I haven't the foggiest idea *what* you're trying to do.
> >However, this might help clarify things.
> 
> My apologies. I'll try to be more precise (the example didn't
> illustrate well what I wanted to do):
> 
> I have a derived class that I want to make instances of. I want to be
> able to change the base class (hence the alias) and I can't just
> create an instance of the base class and forget all about the derived
> class, because the derived class inherits from another class as well.
> So the derived class instance acts like a base class instance with the
> extra functionality as defined in the other class. The def looks like
> this:
> class DerivedClass(BaseClassAlias, SomeOtherClass):
> 
> I do not even know if I'm searching in the right direction to get to
> above mentioned result. I guess I could change the base class alias
> and then directly instantiate the derived class. Any other ideas?
> 
> Robin.


   I think the question is "Why do you want to do this?"  You can maybe
get the behavior that you're looking for by messing with the __bases__
attribute of the object's __class__ on the fly, but that sort of thing
is fraught with difficulties.  You're talking about violating some of
the fairly basic ideas of the object-oriented paradigm (such as the
object's type won't change over the lifetime of the object); if you
could describe the problem that you're trying to solve by doing this,
maybe someone could suggest a more straightforward Pythonic approach to
solving the same problem.

  Joshua



More information about the Python-list mailing list