Subclass constructor returns a superclass object

django, python

Solution

`Point.__init__` calls

    super(Point, self).__init__(point, srid=srid)

which calls `GEOSGeometry.__init__`, which calls

    self._post_init(srid)

which executes

    self.__class__ = GEOS_CLASSES[self.geom_typeid]

which changes the class of `self` to `Point`. Evil.

You could try:

class LLPoint(Point):
    def __init__(self, *args, **kwargs):
        ...
        super(LLPoint, self).__init__(lng, lat)   # this changes self.__class__ to Point
        ...
        self.__class__ = LLPoint

Problem

I am specializing `django.contrib.gis.geos.Point` just to better create and handle points with explicity latitude and longitude. My current code: ``` from django.contrib.gis.geos import Point class LLPoint(Point): def __init__(self, *args, **kwargs): lat = kwargs.get('latitude') lng = kwargs.get('longitude') if lat and lng: super(LLPoint, self).__init__(lng, lat) elif lat or lng: raise TypeError(u'You must both declare latitude and longitude, ' 'not just one of them.') else: super(LLPoint, self).__init__(*args, **kwargs) def __unicode__(self): c = self.coordinates() return u'LLPoint Lat: %.5f Lng: %.5f' % (c['latitude'], c['longitude']) def coordinates(self): return { 'latitude': self.coords[1], 'longitude': self.coords[0] } ``` The problem is: ``` >>> LLPoint(latitude=10.0, longitude=20.0) <Point object at 0xdeadbeef> ``` Why it is returning a `Point` object? This way I can't use anything that I declared in the subclass. If I try to reproduce the problem with stub classes, it works. This is probably very stupid, but I can't see.

Original source