[Tutor] dict vs several variables?
Peter Otten
__peter__ at web.de
Sat Feb 18 09:39:55 CET 2012
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
More information about the Tutor
mailing list