What is a mechanism equivalent to "trace variable w ..." in Tcl for Python?

breamoreboy at gmail.com breamoreboy at gmail.com
Fri Sep 30 16:14:48 EDT 2016


On Friday, September 30, 2016 at 7:16:10 PM UTC+1, Les Cargill wrote:
> A really interesting design approach in Tcl is to install a callback
> when a variable is written to. This affords highly event-driven 
> programming.
> 
> Example ( sorry; it's Tcl  ) :
> 
> 
> namespace eval events {
> 	set preRPM -1
> 	proc handleRPM { args } {
> 		# do stuff to handle an RPM change here
> 		variable ::motor::RPM
>                  variable preRPM
> 		puts "RPM changed from $preRPM to $RPM
> 		set preRPM $RPM
>          }
> }
> 
> ...
> 
> trace variable ::motor::RPM w ::events::handleRPM
> 
> ...
> 
> set ::motor::RPM 33.33
> 
> What is an equivalent mechanism in Python?
> 
> Thanks in advance.
> 
> -- 
> Les Cargill

Perhaps you could pinch the idea, or even the code, from tkinter?  E.g. see the section "Variable tracing" at http://stupidpythonideas.blogspot.co.uk/2013/12/tkinter-validation.html

Kindest regards.

Mark Lawrence.



More information about the Python-list mailing list