assignment hook

P

paul.lafollette

Kind people,
Is there any way one can, within Python, intercept the act of
assignment. For instance, suppose that I was obsessed with
FORTRAN II, and decided that I wanted to print a warning,
or raise an exception any time someone assigned an int to a
variable whose name did not start with i,j,k,l,m, or n.

If assigment were an operator, I could, perhaps, subclass
something and override the (non-existant)__assign__ method
to find the name of the variable and the type of the assigned
object, but it ain't so I can't.

My guess is that it is simply impossible, but if it IS possible,
I would love to know how to do it. Thank you for your time
and expertise.

Paul

---------------------------------------
Your task it is, amid confusion, rush, and noise, to grasp the lasting,
calm, and meaningful, and finding it anew, to hold and treasure it.
-Paul Hindemith-

Paul S. LaFollette, Jr.
Temple University
CIS Department
(e-mail address removed)

+1 215 204 6822

www.cis.temple.edu/~lafollet
 
R

Roy Smith

Kind people,
Is there any way one can, within Python, intercept the act of
assignment.

Sure. You just need to define a __setattr__() method for your class. See
http://docs.python.org/ref/attribute-access.html for details.
For instance, suppose that I was obsessed with
FORTRAN II, and decided that I wanted to print a warning,
or raise an exception any time someone assigned an int to a
variable whose name did not start with i,j,k,l,m, or n.

Well, you know the old joke, "You can write Fortran in any language" :)
 
J

John Machin

Roy said:
Sure. You just need to define a __setattr__() method for your class. See
http://docs.python.org/ref/attribute-access.html for details.

Is it possible that at least one of you has the concepts of
"assignment" and "binding" a little mixed?
Well, you know the old joke, "You can write Fortran in any language" :)

Yes, but it's a bit difficult to express old FORTRAN jokes in another
language:

real = float
integer = int
assert isinstance(God, real) and isinstance(Jesus, integer)

Cheers,
John
 
G

Georg Brandl

Kind people,
Is there any way one can, within Python, intercept the act of
assignment. For instance, suppose that I was obsessed with
FORTRAN II, and decided that I wanted to print a warning,
or raise an exception any time someone assigned an int to a
variable whose name did not start with i,j,k,l,m, or n.

This is not possible. Assignments in the form

name = value

(also called "binding a name") are not overloadable. As Roy Smith already noted,
assignments in the form

name.attribute = value
name[item] = value

are overloadable via __setattr__ and __setitem__.

Georg
 
F

Fredrik Lundh

Georg said:
This is not possible. Assignments in the form

name = value

(also called "binding a name") are not overloadable.

footnote: unless you execute the code in a controlled environment:

class mydict(dict):
def __setitem__(self, key, value):
print "SET", key, value
self.__dict__[key] = value

ns = mydict()

exec "a = 10" in ns

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top