P3trus
P3trus

Reputation: 7265

attribute naming, tuple vs multiple names

I was thinking about parts of my class api's and one thing that came up was the following: Should I use a tuple/list of equal attributes or should I use several attributes, e.g. let's say I've got a Controller class which reads several thermometers.

class Controller(object):
    def __init__(self):
        self.temperature1 = Thermometer()
        self.temperature3 = Thermometer()
        self.temperature2 = Thermometer()
        self.temperature4 = Thermometer()

vs.

class Controller(object):
    def __init__(self):
        self.temperature = tuple(Thermometer() for _ in range(4))

Is there a best practice when I should use which style?

(Let's assume the number of Thermometers will not be changed, otherwise choosing the second style with a list would be obvious.)

Upvotes: 0

Views: 159

Answers (2)

Andy Hayden
Andy Hayden

Reputation: 375725

Another option is to store them as a dictionary:

{1: temp1, 2: temp2}

The most important thing in deciding how to store data is relaying the data's meaning, if these items are essentially the same information in a slightly different context then they should be grouped (in terms of data-type) to relay that - i.e. they should be stored as either a tuple or a dictionary.

Note: if you use a tuple and then later insert more data, e.g. a temp0 at the beginning, then there could be backwards-compatability issues where you've grabbed individual variables. (With a dictionary temp[1] will always return temp1.)

Upvotes: 0

Gareth Latty
Gareth Latty

Reputation: 89087

A tuple or list, 100%. variable1, variable2, etc... is a really common anti-pattern.

Think about how you code later - it's likely you'll want to do similar things to these items. In a data structure, you can loop over them to perform operations, with the numbered variable names, you'll have to do it manually. Not only that but it makes it easier to add in more values, it makes you code more generic and therefore more reusable, and means you can add new values mid-execution easily.

Why make the assumption the number will not be changed? More often than not, assumptions like that end up being wrong. Regardless, you can already see that the second example exemplifies the do not repeat yourself idiom that is central to clear, efficient code.

Even if you had more relevant names eg: cpu_temperature, hdd_temperature, I would say that if you ever see yourself performing the same operations on them, you want a data structure, not lots of variables. In this case, a dictionary:

temperatures = {
    "cpu": ...,
    "hdd": ...,
    ...
}

The main thing is that by storing the data in a data structure, you are giving the software the information about the grouping you are providing. If you just give them the variable names, you are only telling the programmer(s) - and if they are numbered, then you are not even really telling the programmer(s) what they are.

Upvotes: 4

Related Questions