data strucutures in python

Donn Cave donn at u.washington.edu
Tue Sep 19 19:18:04 EDT 2000


Quoth "Matthew Banham" <matb at photond.com>:

| I'm new to Python and I was wondering if anyone new of a way of doing the
| following:-
|
| I have a list of tuples of the form (variablename, variableFunction).
| In Python, if I type:-
|
| variablename = data then variableFunction gets called with data passed to
| it. For example say I have:-
|
| p = 'lambda', ChangeLambdaFunc
|
| and I type the line:-
|
| lambda = 10
|
| then the above line gets implemented as:-
|
| ChangeLambdaFunc(10)

That doesn't just happen to be unsupported by Python, there's a hint
of alien weirdness to it.  Not because something's happening by magic
(that's all too common in Python) but because it looks like the variable
is an object with properties, that can have a value assigned to it.

In Python, the left hand side of those assignments is only a name, and
each name has only one property, the object assigned to it.  So what
you want, in these terms, is a namespace where you can intervene in
the assignment of names.

The namespace where you type "lambda = 10" is not, so far as I know,
a place where you can do this, but you can in a class instance, with
the __setattr__() method.  Now we're talking more like

    self.lambda = 10     -->   self.ChangeLambdaFunc(10)

If that will work for you, there's hope.  It's awfully unusual, though -
I was going to say look at the distribution library for examples, but
in all those 137 or so modules I don't see a single use of __setattr__.
It's not terribly easy to use, either - remember, it applies to the
whole namespace.

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list