Strategies for controling attribute assignment

David Bolen db3l at fitlinxx.com
Tue Oct 2 16:45:53 EDT 2001


Dale Strickland-Clark <dale at riverhall.NOSPAMco.uk> writes:

> The problem isn't with handling the database assignments. It's the
> fact that all instance assignments (self.anything) has to go through
> the same code. This is such an ugly overhead.
> 
> Throughout the class, there are dozens of uses of instance variables
> which are forced through this code. I don't want to know about them.

But I think that's what Steve's suggestion helps you avoid.  By
placing all the database assignments that you do need to monitor into
a contained class, it's only that contained class that needs
__setattr__, and thus your normal instance assignments aren't touched.
And that contained class has no instance variables other than those
that are relevant for the database it represents.

> I guess I'd like to explicitly declare which attributes are part of
> the public face of the class and which are not.

The same suggestion can also help with this problem.  Call the
instance of the contained database class "public", and then all users
of your class will be accessing database query/assignments with
references such as "object.public.value"

I suppose if you really wanted to (e.g., users of your object just had
to be able to reference object.value), you could invert this.  Leave
all instance variables as just for the database, and place all of your
prior private instance variables into a contained object - let's call
it "private".  You'd end up using "self.private.xxxx" instead of just
"self.xxx" inside your object.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list