[Tutor] creating a subclass from superclass without __init__
Matt Gregory
matt.gregory at oregonstate.edu
Fri Aug 24 20:22:13 CEST 2012
Is it possible to create a subclass of a superclass that doesn't have an
__init__ and is only created through another class. Here is an example
of what isn't working:
class Spam(object):
def __new__(cls, *args):
return super(Spam, cls).__new__(cls, args)
def __init__(self):
raise AttributeError('Cannot create Spam')
def test(self):
print 'This is a Spam class'
class SpamMaker(object):
def make_spam(self):
return Spam.__new__(Spam)
class SubSpam(Spam):
def __new__(cls, *args):
return SpamMaker().make_spam()
def test(self):
print 'This is a SubSpam class'
b = SpamMaker().make_spam()
b.test()
c = SubSpam()
c.test()
prints
This is a Spam class
This is a Spam class
I know that I'm not creating the SubSpam instance correctly, but I have
no idea how to do it. It's probably something very obvious that I'm
missing. My real use case is using the Python bindings to GDAL and
trying to create a subclass of gdal.Band which can't be instantiated
directly.
thanks, matt
More information about the Tutor
mailing list