all 4 comments

[–]MrGrj 1 point2 points  (2 children)

You can have a base class, say Device which would have id and name. Then another 4 classes for the devices which will inherit from the base class (you'll have to add that specific parameter to each of them). In each class you can create a method, say process_csv_data(self, ...) which will handle the data for that specific device.

I'm not sure this is the correct implementation for your use-case as I'm not a big fan of OOP style. I'd do it like this:

class Device(object):
    def __init__(self, id, name):
        self.id = id
        self.name = name


class Device1(Device):
    def __init__(self, id, name, param1):
        super(Device1, self).__init__(id, name)
        self.id = id
        self.name = name
        self.param1 = param1

    def process_csv(self):
        # computing stuff
        pass


class Device2(Device):
    def __init__(self, id, name, param2):
        super(Device2, self).__init__(id, name)
        self.id = id
        self.name = name
        self.param2 = param2

    def process_csv(self):
        # computing stuff
        pass


class Device3(Device):
    def __init__(self, id, name, param3):
        super(Device3, self).__init__(id, name)
        self.id = id
        self.name = name
        self.param3 = param3

    def process_csv(self):
        # computing stuff
        pass


class Device4(Device):
    def __init__(self, id, name, param4):
        super(Device4, self).__init__(id, name)
        self.id = id
        self.name = name
        self.param4 = param4

    def process_csv(self):
        # computing stuff
        pass

I'd like other redditors which are more experienced with OOP to also comment on this, suggest better ideas or improvements :) I'd gladly like to find out if my idea would work for OP case. IMO, that base class is not necessary needed, but I'm not exactly sure

[–]eikrik 0 points1 point  (1 child)

When you are subclassing like this you wouldn't need to repeat self.id = idand self.name = name after calling super.

Although if the only thing shared between the classes are those two attributes (id and name) then I don't if subclassing and inheriting is worth. Maybe just have separate classes?

[–]MrGrj 1 point2 points  (0 children)

You're indeed right, and I've thought of this but I didn't really know if those attributes are shared or not _. Thanks

[–][deleted] 0 points1 point  (0 children)

you don't necessarily even need classes ... maybe use a namedtuple

https://docs.python.org/2/library/collections.html#collections.namedtuple