检测python变量值的变化 [英] Detecting change in the value of python variables

查看:979
本文介绍了检测python变量值的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么方法可以在python中每次调用一个变量的值呢?



类似于监听器,或许?

具体来说,我指的是
只有变量在GAE会话等脚本之间共享
。 (使用Cookies,Memcache等共享数据)

示例:
ScriptA& ScriptB,共享一个会话变量。当脚本B改变它时,SctiptA必须调用一个方法来处理这个改变。

使用属性

使用属性。

  class某物(对象):
@property
def some_value(self):
返回self._actual
@ some_value.setter
def some_value(self,value):
printsome_value changed to,value
self ._actual =值


Is there some way to call a function each-time the value of a variable changes, in python?

Something like a listener, perhaps?

Specifically, I'm referring to the case where only the variable is shared between scripts like a GAE-Session. (Which uses Cookies, Memcache, etc to share data)

Example: ScriptA & ScriptB, share a session variable. When Script B changes it, SctiptA has to call a method to handle that change.

解决方案

Use properties. Only mutable values can change to begin with.

class Something(object):
    @property
    def some_value(self):
        return self._actual
    @some_value.setter
    def some_value(self, value):
        print "some_value changed to", value
        self._actual = value

这篇关于检测python变量值的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆