Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python类实例变量的自动函数调用?_Python_Function_Class_Variables_Instance - Fatal编程技术网

Python类实例变量的自动函数调用?

Python类实例变量的自动函数调用?,python,function,class,variables,instance,Python,Function,Class,Variables,Instance,假设我有一个叫做Car的类,如下所示: class Car(object): """Car example with weight and speed.""" def __init__(self): self.weight = None self.speed = None 如果我将汽车初始化为空对象: red_car = Car() 我加了一个速度和一个重量: red_car.speed = 60 red_car.weight = 3500

假设我有一个叫做
Car
的类,如下所示:

class Car(object):
    """Car example with weight and speed."""
    def __init__(self):
        self.weight = None
        self.speed = None
如果我将
汽车
初始化为空对象:

red_car = Car()
我加了一个
速度
和一个
重量

red_car.speed = 60
red_car.weight = 3500
这很好,但是如果我想在尝试将这些变量添加到实例时运行函数呢?像这样的功能:

def change_weight(self, any_int):
    return (any_int - 10)
问题是,每当我尝试向对象添加特定实例变量时,我希望它自动运行此函数。如果可能,我希望它只在
weight
实例变量上运行
change\u weight


我是否正确理解了这一点,还是应该通过函数单独运行整数,然后手动添加到对象中?

是否要使用属性

class Car(object):
    """Car example with weight and speed."""
    def __init__(self):
        self._weight = None # internal value
        self.speed = None

    @property
    def weight(self):
        return self._weight

    @weight.setter
    def weight(self, the_weight):
        self._weight = the_weight - 10 # or whatever here

现在,您可以正常设置
速度
;执行
car.weight=20
时,将调用setter函数,并将实际重量设置为
20-10=10

您要使用的属性

class Car(object):
    """Car example with weight and speed."""
    def __init__(self):
        self._weight = None # internal value
        self.speed = None

    @property
    def weight(self):
        return self._weight

    @weight.setter
    def weight(self, the_weight):
        self._weight = the_weight - 10 # or whatever here

现在,您可以正常设置
速度
;当您执行
car.weight=20
时,将调用setter函数,并将实际权重设置为
20-10=10

阅读Python的概念。特别是,听起来您想要定义一个自定义的
\uuuu set\uuu()
方法

例如,
obj.d
obj
的字典中查找
d
。如果
d
定义了方法
\uuuu get\uuuu()
,则根据下面列出的优先规则调用
d.\uu get\uuuj(obj)

或者,您可能会发现使用(特定类型的描述符)更容易一些

property()
是一种简洁的方法,用于构建数据描述符,在访问属性时触发函数调用


一旦你理解了描述符的行为,它就相当合乎逻辑了,但它的行为并不像你想象的那样;特别是,
\uuuuuu get\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。因此,请看上面的一些示例,并在深入研究之前仔细研究它们。

阅读Python的概念。特别是,听起来您想要定义一个自定义的
\uuuu set\uuu()
方法

例如,
obj.d
obj
的字典中查找
d
。如果
d
定义了方法
\uuuu get\uuuu()
,则根据下面列出的优先规则调用
d.\uu get\uuuj(obj)

或者,您可能会发现使用(特定类型的描述符)更容易一些

property()
是一种简洁的方法,用于构建数据描述符,在访问属性时触发函数调用


一旦你理解了描述符的行为,它就相当合乎逻辑了,但它的行为并不像你想象的那样;特别是,
\uuuuuu get\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。因此,请看上面的一些示例,并在深入研究之前仔细研究它们。

这似乎是一个非常适合getter和setter的用例。@GWW,Noooo。这不是PythonJava@gnibbler:对不起,我应该说
properties
这似乎是getter和setter的完美用例。@GWW,Noooo。这不是PythonJava@gnibbler:对不起,我应该一直说
properties
。。等一下,让我看看我是否理解正确。。您正在使用decorator
property
声明名为
weight
的函数,但它正在返回实例
\u weight
(为什么是私有值?为什么不仅仅是
weight
?然后你有另一个函数名为
weight
,但它上面有一个名为
weight.setter
的装饰器,我假设,因为当我执行
car.weight=20
时,它会运行这个函数?而装饰器
weight.setter
会抓住另一个
weight
函数在
weight.setter
函数完成后,将其作为一个返回?在大多数情况下,它是有意义的。我只是想澄清一下,我上面写的是正确的,以及关于必须使用
将其作为一个私有变量的整件事。所有这些时间..等一下,让我看看我是否正确理解了这一点..你是对的将名为
weight
的函数与decorator
property
关联,但它将返回实例
\u weight
(为什么是私有值?为什么不仅仅是
weight
?然后你有另一个函数名为
weight
,但它上面有一个名为
weight.setter
的装饰器,我假设,因为当我执行
car.weight=20
时,它会运行这个函数?而装饰器
weight.setter
会抓住另一个
weight
函数在
weight.setter
函数完成后,将其作为一个返回?在大多数情况下,它是有意义的。我只是想澄清一下,我上面写的内容是正确的,以及关于必须使用
将其作为私有变量的整件事。有趣!可以!有趣!可以!