Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Can you create an instance of a subclass with an existing instance of the base class?

Reply
Thread Tools

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

 
 
Sandra-24
Guest
Posts: n/a
 
      04-21-2006
Can you create an instance of a subclass using an existing instance of
the base class?

Such things would be impossible in some languages or very difficult in
others. I wonder if this can be done in python, without copying the
base class instance, which in my case is a very expensive object.

Any ideas?

Thanks,
-Sandra

 
Reply With Quote
 
 
 
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      04-21-2006
In article <(E-Mail Removed) .com>,
"Sandra-24" <(E-Mail Removed)> wrote:

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


I think you're taking Python's OO-ness too seriously. One of the
strengths of Python is that it can _look_ like an OO language without
actually being OO.
 
Reply With Quote
 
 
 
 
Cavingdeep
Guest
Posts: n/a
 
      04-22-2006
With new-style classes you can find out a class' subclasses and then
you can instantiate the subclass you want. Suppose you have two classes
A and B, B is a subclass of A, A is a new-style class. Now you have an
A's instance called "a", to instance B you can do the following:

b = a.__class__.__subclasses__()[0]()

 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      04-22-2006
Sandra-24 wrote:

> Can you create an instance of a subclass using an existing instance of
> the base class?
>
> Such things would be impossible in some languages or very difficult in
> others. I wonder if this can be done in python, without copying the
> base class instance, which in my case is a very expensive object.


You can change the class of an instance by assigning to the __class__
attribute. The new class doesn't even need to be a subclass of the old:

>>> class A(object):

.... def __init__(self, name):
.... self.name = name
.... def show(self): print self.name
....
>>> a = A("alpha")
>>> a.show()

alpha
>>> class B(object):

.... def show(self): print self.name.upper()
....
>>> a.__class__ = B
>>> a.show()

ALPHA

Peter
 
Reply With Quote
 
Sandra-24
Guest
Posts: n/a
 
      04-22-2006
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.

In this case it doesn't work.

TypeError: __class__ assignment: only for heap types

I suspect that's because this object begins its life in C code.

The technique of using the __class__.__subclasses__ also fails:

TypeError: cannot create 'B' instances

This seems more complex than I thought. Can one do this for an object
that beings it's life in C?

Thanks,
-Sandra

Peter Otten wrote:
> Sandra-24 wrote:
>
> > Can you create an instance of a subclass using an existing instance of
> > the base class?
> >
> > Such things would be impossible in some languages or very difficult in
> > others. I wonder if this can be done in python, without copying the
> > base class instance, which in my case is a very expensive object.

>
> You can change the class of an instance by assigning to the __class__
> attribute. The new class doesn't even need to be a subclass of the old:
>
> >>> class A(object):

> ... def __init__(self, name):
> ... self.name = name
> ... def show(self): print self.name
> ...
> >>> a = A("alpha")
> >>> a.show()

> alpha
> >>> class B(object):

> ... def show(self): print self.name.upper()
> ...
> >>> a.__class__ = B
> >>> a.show()

> ALPHA
>
> Peter


 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      04-22-2006
In article <(E-Mail Removed). com>,
"Sandra-24" <(E-Mail Removed)> 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.
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      04-23-2006
Sandra-24 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.
>
> In this case it doesn't work.
>
> TypeError: __class__ assignment: only for heap types
>
> I suspect that's because this object begins its life in C code.
>
> The technique of using the __class__.__subclasses__ also fails:
>
> TypeError: cannot create 'B' instances
>
> This seems more complex than I thought. Can one do this for an object
> that beings it's life in C?


The restriction originates in the metaclass. Perhaps you can use a
customized metaclass that allows subclass assignment, but I don't know
enough about Python's internals to tell you how/whether that is possible.

An alternative might be that you always start out with a subclass coded in
Python:

>>> abc = tuple("abc")
>>> abc.__class__ = tuple

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: __class__ assignment: only for heap types


>>> class Tuple(tuple): pass

....
>>> class Subclass(Tuple):

.... first = property(lambda self: self[0])
....
>>> abc = Tuple("abc")
>>> abc.__class__ = Subclass
>>> abc.first

'a'

In the example a Tuple should be able to do everything a tuple can do;
because Tuple is coded in Python you can also change the __class__.

Peter



 
Reply With Quote
 
Sandra-24
Guest
Posts: n/a
 
      04-23-2006
Lawrence D'Oliveiro wrote:
> In article <(E-Mail Removed). com>,
> "Sandra-24" <(E-Mail Removed)> 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

 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      04-24-2006
In article <(E-Mail Removed). com>,
"Sandra-24" <(E-Mail Removed)> wrote:

>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.


All you want is a dictionary, then. That's basically what Python objects
are.
 
Reply With Quote
 
bruno at modulix
Guest
Posts: n/a
 
      04-24-2006
Lawrence D'Oliveiro wrote:
(snip)
> I think you're taking Python's OO-ness too seriously. One of the
> strengths of Python is that it can _look_ like an OO language without
> actually being OO.


According to which definition of OO ?

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '(E-Mail Removed)'.split('@')])"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
subclass a class in the namespace of the that subclass Trans Ruby 8 10-23-2008 07:24 AM
Bind an instance of a base to a subclass - can this be done? Lou Pecora Python 8 05-25-2006 03:00 PM
String subclass method returns subclass - bug or feature? S.Volkov Ruby 2 03-12-2006 06:46 PM
subclass has a variable that is subclass of same superclass jstorta Java 3 02-20-2006 08:42 PM
Problem when subclass instance changes base class instance variable Gerry Sutton Python 1 04-16-2005 06:06 AM



Advertisments