自动更新R参考类中的字段(数据成员) [英] Auto update of a field (data member) in R reference class

查看:85
本文介绍了自动更新R参考类中的字段(数据成员)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否与python中的decorator @property等效?

Any equivalent as the decorator @property in python?

由于要更新另一个字段,因此要使一个字段自动更新(在访问时),而不是在访问之前重新计算它.

That is to have one field automatically updated (on access) due to an update of another field, instead of recomputing it before access.

更新2014-02-06

通过将一个"字段定义为另一个"字段的activeBindingFunction,可以打开自动更新(请参阅@jdharrison的答案).但是,是否有任何方法可以检查这种更新是否为延迟评估?如果没有,我们如何实现呢?

By defining "one" field as an activeBindingFunction of "another" field turns on the automatic updates (See @jdharrison's answer). However, is there anyway to check whether such updates are lazy evaluation? If not, how can we achieve this?

(后来的activeBindingFunction内部的非正式"打印功能很明显评估是懒惰的.是否可以更正式地检查此问题?)

(A later edit: an "informal" print function inside the activeBindingFunction is evident that the evaluation is lazy. Any way to check this issue in a more formal manner?)

为了更好地说明我的问题,下面是一段通过decorator @property产生所需的懒惰行为的python代码-请耐心把它放在带有 R 标签的问题中.

For better illustration of my question, below is a piece of python code producing the desired lazy behavior via decorator @property - bear with me to put it in an R-tagged question.

class Test(object):
    def __init__(self):
        self._num1=None
        self._num2=None

    @property
    def num1(self):
        if (self._num1 is None):
            self._num1=1
            return(self.num1)
        else:
            return(self._num1)

    @num1.setter
    def num1(self, value):
        self._num1 = value

    @property
    def num2(self):
        self._num2=self._num1*2
        return(self._num2)

交互式会话

In [2]: obj=Test()

In [3]: obj.__dict__
Out[3]: {'_num1': None, '_num2': None}

In [4]: obj.num1
Out[4]: 1

In [5]: obj.__dict__
Out[5]: {'_num1': 1, '_num2': None}

In [6]: obj.num2
Out[6]: 2

In [7]: obj.__dict__
Out[7]: {'_num1': 1, '_num2': 2}

In [8]: obj.num1 = 5

In [9]: obj.__dict__
Out[9]: {'_num1': 5, '_num2': 2}

In [10]: obj.num2
Out[10]: 10

In [11]: obj.__dict__
Out[11]: {'_num1': 5, '_num2': 10}

推荐答案

不确定它是否在找你

test <- setRefClass("test",
                            fields   = list(
                                            num1 = "numeric"
                                            , num2 = function(){
                                                                print("hello")
                                                                 2*.self$num1
                                                                }
                                           ),
                            methods  = list(
                              initialize = function(num1 = 1){
                                num1 <<- num1
                              },

                              show = function(){
                                print(c(num1 = num1, num2 = num2))
                              }
                            )
)

> tt <- test()
> tt
[1] "hello;"
num1 num2 
   1    2 
> tt$num1<-4
> tt
[1] "hello;"
num1 num2 
   4    8 

这篇关于自动更新R参考类中的字段(数据成员)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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