[Tutor] designing POOP

bhaaluu bhaaluu at gmail.com
Thu Feb 7 20:52:53 CET 2008

I was asked:


Here's a situation I often encounter, and I was wondering what the
"best practice" is.  I've generally initialized my classes' attributes
this same way:

> class TestClass1(object):
>     """ please give me a better name"""
>     def __init__(self):
>         """please document me"""
>         self.name = ""
>         self.answer = ""
>         self.strength = 20
>         self.wealth = 45
>         self.light = 0
>         self.tally = 0

but it could also be done like so:

> class TestClass1(object):
>     """ please give me a better name"""
>         name = ""
>         answer = ""
>         strength = 20
>         wealth = 45
>         light = 0
>         tally = 0
>     def __init__(self,name="Zed"):
>         """please document me"""
>         self.name = name
>         ...etc.

I realize that the two are NOT equivalent if you're using the class as
a static class, rather than instantiating it (e.g. using a static
class called Global while weaning oneself from global variables...)
However, I'm asking about this present case: the class under
discussion will always be instantiated.  It seems to me that declaring
the attributes in the class body makes the class more amenable to
introspection, but are there downsides I'm not seeing?  What's the
best practice?


I've tried both ways and can't see any difference between
the two as far as input/output is concerned.

Best practice?
b h a a l u u at g m a i l dot c o m
"You assist an evil system most effectively by obeying its
orders and decrees. An evil system never deserves such
allegiance.  Allegiance to it means partaking of the evil.
A good person will resist an evil system with his or her
whole soul." [Mahatma Gandhi]

More information about the Tutor mailing list