[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Logically/Selectively Sub Class?

xkenneth

3/9/2008 9:28:00 PM

Might be a silly question, but is it possible to selectively subclass,
IE subclass is a supporting module is present and not otherwise.

Regards,
Kenneth Miller
4 Answers

Diez B. Roggisch

3/9/2008 9:38:00 PM

0

xkenneth schrieb:
> Might be a silly question, but is it possible to selectively subclass,
> IE subclass is a supporting module is present and not otherwise.

Yes, something like this should work

class Foo(some, base, classes)
pass

if condition:
Temp = Foo
class Foo(Temp, otherclass): pass


Alternatively, you can use the type-function to create classes explicit
with a list of base-classes.

However it smells after bad design... what's your actual usecase?

Diez

xkenneth

3/9/2008 9:44:00 PM

0

On Mar 9, 4:38 pm, "Diez B. Roggisch" <de...@nospam.web.de> wrote:
> xkenneth schrieb:
>
> > Might be a silly question, but is it possible to selectively subclass,
> > IE subclass is a supporting module is present and not otherwise.
>
> Yes, something like this should work
>
> class Foo(some, base, classes)
>     pass
>
> if condition:
>     Temp = Foo
>     class Foo(Temp, otherclass): pass
>
> Alternatively, you can use the type-function to create classes explicit
> with a list of base-classes.
>
> However it smells after bad design... what's your actual usecase?
>
> Diez

Yeah, it's really a non-issue, just more a curiosity. I'm using ZODB
for a lot of my stuff now, and I need my objects to be persistent,
however there might be a case where my classes could be used in a non-
persistent manner. I'll just have ZODB as a requirement for my
package.

Regards,
Kenneth Miller

Aaron Brady

3/9/2008 9:45:00 PM

0

On Mar 9, 4:28 pm, xkenneth <xkenn...@gmail.com> wrote:
> Might be a silly question, but is it possible to selectively subclass,
> IE subclass is a supporting module is present and not otherwise.
>
> Regards,
> Kenneth Miller

if mod is present:
class Waypoint( Mine, Yours ): pass
else:
class Waypoint( Mine ): pass

class NewClass( Waypoint ).

In 3.0, you can do

if mod is present:
bases= Mine, Yours
else:
bases= Mine,

class NewClass( *bases ).

Diez B. Roggisch

3/9/2008 9:56:00 PM

0

xkenneth schrieb:
> On Mar 9, 4:38 pm, "Diez B. Roggisch" <de...@nospam.web.de> wrote:
>> xkenneth schrieb:
>>
>>> Might be a silly question, but is it possible to selectively subclass,
>>> IE subclass is a supporting module is present and not otherwise.
>> Yes, something like this should work
>>
>> class Foo(some, base, classes)
>> pass
>>
>> if condition:
>> Temp = Foo
>> class Foo(Temp, otherclass): pass
>>
>> Alternatively, you can use the type-function to create classes explicit
>> with a list of base-classes.
>>
>> However it smells after bad design... what's your actual usecase?
>>
>> Diez
>
> Yeah, it's really a non-issue, just more a curiosity. I'm using ZODB
> for a lot of my stuff now, and I need my objects to be persistent,
> however there might be a case where my classes could be used in a non-
> persistent manner. I'll just have ZODB as a requirement for my
> package.

Ah. Then this also might work

try:
from ZODB import Persistent
except ImportError:
class Persistent: pass

Diez