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

Les Cargill lcargill99 at comcast.com
Fri Sep 30 14:23:29 EDT 2016


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



More information about the Python-list mailing list