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

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to