[Tutor] Fwd: designing POOP

Marc Tompkins marc.tompkins at gmail.com
Thu Feb 7 21:36:18 CET 2008


Sorry, my bad - this was my me, but I forgot to hit "Reply All".

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?

Standing by to get flamed...
-- 
www.fsrtechnologies.com


More information about the Tutor mailing list