On 25/08/12 06:03, Matt Gregory wrote:

There are two classes of interest in GDAL - a Dataset which typically
describes a geospatial raster file and Band which described a single band
 from the Dataset. gdal.Band just raises an AttributeError within __init__,
but a gdal.Band instance can be created using
gdal.Dataset.GetRasterBand(bandNum).

What a nasty, horrible, pointless restriction. Is there any reason why you
need to follow it?


I had wanted to create additional methods on gdal.Band that would allow
some GIS type operations (e.g. overloading __add__), thus I thought I would
 need a subclass of gdal.Band. So I was unsure of how to get an instance of
my subclass when the only way I knew how to create an instance of the
superclass (gdal.Band) was through the call to GetRasterBand().


class Band(object):
    def __new__(cls, *args):
        return super(Band, cls).__new__(cls, *args)
    def __init__(cls, *args):
        # Silly booby-trap.
        raise AttributeError('not only is this a pointless restriction '
            'on how you create instances, but this is the wrong '
            'exception type too!')

def GetRasterBand(*args):
    return Band.__new__(Band, *args)

class MyBand(Band):
    def __init__(self, *args):
        # Override the silly booby trap.
        pass
    def __add__(self, other):
        return 'something...'
    __rand__ = __add__



Works for me.



Does that make any sense in relation to my contrived example? In my
 example, I didn't really want to create a Spam instance from the
SubSpam __new__, I just didn't know how to return a SubSpam instance
 that was forced to go through SpamMaker.make_spam().

I don't think you can, but I don't think you need to.



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

Reply via email to