Re: Subclass dynamically

2009-08-19 Thread Piet van Oostrum
 Robert Dailey rcdai...@gmail.com (RD) wrote:

RD Hey,
RD I have a class that I want to have a different base class depending on
RD a parameter that I pass to its __init__method. For example
RD (pseudocode):

RD class MyDerived( self.base ):
RD   def __init__( self, base ):
RD self.base = base


RD Something like that... and then I would do this:

RD foo = MyDerived( MyBase() )

What do you want? As you write it now foo would be an instance of
MyDerived, but you say you want to have a class with a different base
class...

So does this mean that foo should become that class or that foo should
become an instance of a new anonymous class that has a specified base
class?

And on the other hand is MyBase the required base class. But you pass an
instance of MyBase, not MyBase itself. As you have it above MyBase()
should be a class, therefore MyBase should be a metaclass. Or is that
not what you want?
-- 
Piet van Oostrum p...@cs.uu.nl
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Subclass dynamically

2009-08-18 Thread Sergey Simonenko
On Sat, 08 Aug 2009 11:12:30 -0600, Robert Dailey rcdai...@gmail.com  
wrote:


How can you subclass a class from an object?..


Hey,

I have a class that I want to have a different base class depending on
a parameter that I pass to its __init__method. For example
(pseudocode):

class MyDerived( self.base ):
  def __init__( self, base ):
self.base = base


Something like that... and then I would do this:

foo = MyDerived( MyBase() )

Note I'm using Python 3.1 on Windows. Thanks in advance.



--
Kind regards, Sergey Simonenko.

--
http://mail.python.org/mailman/listinfo/python-list


Re: Subclass dynamically

2009-08-08 Thread Gary Herron

Robert Dailey wrote:

Hey,

I have a class that I want to have a different base class depending on
a parameter that I pass to its __init__method. For example
(pseudocode):

class MyDerived( self.base ):
  def __init__( self, base ):
self.base = base


Something like that... and then I would do this:

foo = MyDerived( MyBase() )

Note I'm using Python 3.1 on Windows. Thanks in advance.
  
Python makes it possible to change base classes, but that doesn't mean 
it ever a good idea.


Moreover, the assignment is at the class level, not the instance level:

   MyDerived.__bases__ = (base,) #A tuple of bases

would change the base class for the class MyDerived, and all instances 
past, present, or future.


Better (but still not good) might be a factory function that derives a 
class with the desired base class:


def Derive(base):
   class Derived(base):
   pass
   return Derived

DerivedClass = Derive(MyBase)
foo = DerivedClass(...)

I believe that will produce what you were looking for.

Gary Herron

--
http://mail.python.org/mailman/listinfo/python-list


Re: Subclass dynamically

2009-08-08 Thread Luis Alberto Zarrabeitia Gomez

Quoting Robert Dailey rcdai...@gmail.com:

 Hey,
 
 I have a class that I want to have a different base class depending on
 a parameter that I pass to its __init__method. For example
 (pseudocode):

1- Are you sure that you want that behavior? Given that in python, a class is
just a particular case of invocable object, you could just write a function
instead, and the code may be more robust.

2- If you are /sure/ that is the behavior you want, take a look at the __new__
method. The __new__ can decide which instance to return (you could even define a
new class inside of it, instantiate it, and return the instance).

3- You may want to take a look at metaclasses. But without more details about
why you want it, I can't give you more precise answers.

Regards,

-- 
Luis Zarrabeitia
Facultad de Matemática y Computación, UH
http://profesores.matcom.uh.cu/~kyrie



-- 
Participe en Universidad 2010, del 8 al 12 de febrero de 2010
La Habana, Cuba 
http://www.universidad2010.cu

-- 
http://mail.python.org/mailman/listinfo/python-list