2

I'm writing some code and trying to break it into classes as I go. I'm pretty new to OOP and am having some trouble getting what I've written to work. I'm having to write it in Python 2.7 as some of the packages I'm using later don't have Python 3 compatibility.

Am I going overboard on the inheritance? I basically want the Timeseries class to be able to use all variables and parameters attached to the Reanalysis object, but only get the node_coordinates back.

class Reanalysis():
    """Initiates a reanalysis object"""

    def __init__(self, latitude, longitude, resolution):
        self.datetime_format = '%Y-%B-%d %H:%M'
        self.lat = latitude
        self.lon = longitude
        self.res = resolution
        self.node_coordinates = Timeseries().node_coordinates

class Timeseries(Reanalysis):

    def __init__(self):
        super(Timeseries, self).__init__()
        self.get_node_coordinates()

    def get_node_coordinates(self):
        """Determines node coordinates
        """

        latres = self.res
        lonres = self.res
        self.node_coordinates = set([(latres * np.floor(self.lat / latres),
                                  lonres * np.floor(self.lon / lonres)),
                                 (latres * np.ceil(self.lat / latres),
                                  lonres * np.floor(self.lon / lonres)),
                                 (latres * np.floor(self.lat / latres),
                                  lonres * np.ceil(self.lon / lonres)),
                                 (latres * np.ceil(self.lat / latres),
                                  lonres * np.ceil(self.lon / lonres))])

When I try to compile I get the error:

super(Timeseries, self).__init__()
TypeError: must be type, not classobj

Thanks

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
WRJ
  • 617
  • 3
  • 9
  • 19

2 Answers2

0

I think your problem here is that you haven't defined your classes as 'new-style' classes (i.e. ones which inherit from 'object'). Try changing the declaration to

class Reanalysis(object):

That should allow it to interact with super(...) properly.

You'll also need to match the argument lists for the superclass init. The superclass takes 3 arguments, which you need to pass in.

sisyphus
  • 6,174
  • 1
  • 25
  • 35
  • I've just tried it and the error changes to TypeError: __init__()takes at least 3 arguments (1 given) – WRJ Feb 04 '16 at 12:41
0

The problem lies in:

self.node_coordinates = Timeseries().node_coordinates

The Timeseries class inherits from the Reanalysis one which means that you cannot use Timeseries to initialize a Reanalysis object.

Zachi Shtain
  • 826
  • 1
  • 13
  • 31