New to Python: Features

Andrew Dalke adalke at mindspring.com
Thu Oct 7 20:12:38 CEST 2004


Alex:
> Bully for you -- I don't.  I wish I could set a callback on a given
> object getting modified or a given name getting rebound - but I can't,
> not with enough generality and little-enough black magic to be usable in
> production code.

Hmm..  The times I needed it I used __getattr__ in old-style
classes, sometimes as a proxy.  It seems that properties
should be the appropriate mechanism for new-style classes,
but I've yet to use them in real code.

They don't give the full support that const does.  The
above grey magic doesn't propogate down the attribute accessions
so that a deep structure, like
   a.b.c.d[10]["spam"].z = 1
will be changed even if 'a' is supposed to be const.

I don't know how to handle that case, but I'm hard pressed
to think when I've needed it.

My biggest problem in C++ was all the work needed to
preserve constness.  If I had

Y* fctn(X *x) {
   return x->y;
}

then I needed to duplicate the code to allow
proper const propogation

const Y* fctn(const X* x) {
   return x->y;
}

If I wanted to go through all that work for C++ then
I suppose I could put readonly-style wrappers around
all my Python objects, and add some protocol to ask
a non-const object for its const form.

In either case, too much work for too little gain.

				Andrew
				dalke at dalkescientific.com



More information about the Python-list mailing list