[Tutor] dict vs several variables?
Leam Hall
leamhall at gmail.com
Sat Feb 18 11:23:28 CET 2012
On 02/18/2012 03:39 AM, Peter Otten wrote:
> Leam Hall wrote:
>
>> On 02/17/2012 09:26 AM, Dave Angel wrote:
>>> There are two ways to think of a class. One is to hold various related
>>> data, and the other is to do operations on that data. If you just
>>> consider the first, then you could use a class like a dictionary whose
>>> keys are fixed (known at "compile time").
>>
>> I think a class is the way to go for a couple reasons. First, of course,
>> is that it pushes me to learn more. It also lets me encapsulate a lot of
>> the work into one thing and keep the methods there.
>
> One word of caution. If you make just one do-it-all class with a single
> instance and use instance attributes as if they were global variable the
> improvement over the global dictionary is minimal. You still should strive
> to make dependencies explicit, e. g. prefer
>
> def add(a, b): return a + b
>
> over
>
> class DoItAll:
> def add_a_and_b(self):
> self.c = self.a + self.b
Understood. In this case I'm starting the application and there are two
separate bits. I'm studying tkinter for the class I'm in so the GUI
stuff is one module and the formulas are in another module. The reason I
considered a global dict or a class is because all of the data bits are
directly related.
Dave angel wrote:
class MyDiver(object):
def __init__(self, depth, gasmix, time, rate):
self.depth = int(depth)
self.gasmix = int(gasmix)
self.time = datetime.datetime(time)
self.rate = float(rate)
Which is a pretty good start, makes me think he's done scuba before. :)
The object in question is a scuba dive. The user types in how deep they
want to go, what gas mix they are on, how long they plan on staying, and
what they think their air consumption rate will be. The formulas
calculate absolute atmospheres which are used in other formulas, O2
concentration (a risk factor), how much of the daily increased O2
exposure the dive will create, and how much gas is needed to make that
dive.
If the program grew the only thing that might be pulled out is the air
consumption rate. That would likely go into a diver object so there was
a profile for each diver. However, at the moment the app is just giving
you dive profile information so you can plan your bottom time and gas
consumption.
That's my thinking as of this morning. I need to go back and build
unittests; will do so while moving it into a class. Should be fun and
good learning!
Leam
More information about the Tutor
mailing list