[Tutor] Benefit/Disadvantage to storing data as dictionary vs. in a class

Adam amgaweda at gmail.com
Fri Jun 1 16:45:30 CEST 2012


At least in my own case, the 'self.rooms[z][12][85]' is a means to 
accessing a 'bucket' array for a given room (z), during a given hour 
(12), at a given temperature(85). Each bucket holds, the average, 
variance, standard deviation and an array of previous readings (note, 
readings are not temperatures, these are actually timestamps).

Is there any benefit to creating a class for each bucket (with an 
'update' method) vs. storing those variables as a dictionary with the 
'Room' class as {'var':0, 'avg': 0, 'sd':0, 'readings':[]} and having 
the Room class hold the update function; not so much from good 
programming practices, but more attempting to number of resources the 
class will be using.

Thank you,
Adam

On 6/1/2012 10:24 AM, James Reynolds wrote:
> If you have functions (or in this case methods) which will be used on 
> a certain 'objects' data structure, a class representation of the data 
> is the natural fit. Otherwise, you will need to pass the dictionary 
> around to various functions and this will be seriously confusing.
>
> However, you probably should consider how to represent that data 
> within a class body, that is, it is extremely confusing to access data 
> such as self.rooms[z][12][85][AVG].
>
> Instead, you should break down what the data structure into 
> like-things and how they are related, for example, you might consider 
> something like this:
>
> class Weather(object):
>     def __init__(self, temp, precipitation, humidity, bara_pres):
>         self. temp = temp
>         self. temp = precipitation
>         self. temp = humidity
>         self. temp = bara_pres
>     def my_func(self, *args,**kwargs):
>         pass #do stuff here
>
> However, if you need to either further define the datapoints of if you 
> want the datapoints to be able to "do things" then you could do 
> something like this:
>
> class Temperature(object):
>     def __init__(self, temp):
>         self. temp = temp
>         self.other_stuff_about_temp = stuff
>
>     def my_temp_func(self, *args,**kwargs):
>         pass #do stuff here
>
>
> class Weather(object):
>     def __init__(self, temp, precipitation, humidity, bara_pres):
>         self. temp =  Temperature(temp)
>         self.precipitation = precipitation
>         self.humidity = humidity
>         self.bara_pres = bara_pres
>     def my_func(self, *args,**kwargs):
>         pass #do stuff here
>
> from here you could either put your instantiated objects into a list 
> and do math on the elements thereof, or you could put them into 
> another class object like this:
>
> class WeatherOverTime(object):
>     def __init__(self, my_list):
>         self.my_list =  my_list
>
>     def avg(self, attr): #attr is the attribute you want to take an 
> average of, so humidity, temp, whatever.
>         temp_list = map(lambda x: getattr(x, attr), my_list)
>         return sum(temp_list) / count(temp_list)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120601/25a92912/attachment-0001.html>


More information about the Tutor mailing list