Strategies for controling attribute assignment

Dale Strickland-Clark dale at riverhall.NOSPAMco.uk
Tue Oct 2 23:29:06 CEST 2001


David Bolen <db3l at fitlinxx.com> wrote:

>
>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

It would address the problem, you're right, but it lacks the clean
external appearance of the traditional approach.

If I have to resort to these type of tricks, I think I'd rather use a
'set' method for all public assignment instead.

Actually, I've found that if I don't define __setattr__ until the end
of __init__ , most the the inefficiencies I've been suffering are
avoided.

This also helps avoid some of the initialisation problems when using
__setattr__.

class wibble:
	def __init__(self):
		... stuff...
		__setattr__ = set

	def set(self, attr, value):
		... stuff...
--
Dale Strickland-Clark
Riverhall Systems Ltd



More information about the Python-list mailing list