无法从添加到python类的方法访问属性

2024-09-29 11:30:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我的程序使用MethodType向类添加方法。问题在于,当代码试图访问属性时,它会获取属性对象,而不是属性值。代码如下:

#!/usr/bin/env python3

from types import MethodType

from ev3dev2.sensor import *
from ev3dev2.sensor.lego import LightSensor

# methods that extend the class
def set_calibration(self,min_value,max_value):
    self.min_value = min_value
    self.max_value = max_value
    self.value_range = max_value - min_value

def read_calibrated(self):
    value = self.reflected_light_intensity
    print(value)
    return 100 * ( value - self.min_value ) / self.value_range

LightSensor.set_calibration = MethodType( set_calibration, LightSensor )
LightSensor.read_calibrated = MethodType( read_calibrated, LightSensor )

# create class instance
light_left  = LightSensor(INPUT_2)
light_left.set_calibration( 20, 60 )

print(light_left.reflected_light_intensity)
print(light_left.read_calibrated())

当我运行程序时,它会产生以下输出和错误:

59.1
<property object at 0xb69f3fc0>
Traceback (most recent call last):
  File "./property_test.py", line 27, in <module>
    print(light_left.read_calibrated())
  File "./property_test.py", line 17, in read_calibrated
    return 100 * ( value - self.min_value ) / self.value_range
TypeError: unsupported operand type(s) for -: 'property' and 'int'

我也试着用这个:

def read_calibrated(self):
    # use underscore to get property value
    value = self._reflected_light_intensity
    print(value)
    return 100 * ( value - self.min_value ) / self.value_range

但这产生了一个错误:AttributeError: type object 'LightSensor' has no attribute '_reflected_light_intensity'

那么,回到原始代码,为什么light_left.reflected_light_intensity返回一个数字,而self.reflected_light_intensity返回一个属性对象

更重要的是,如何从read_calibrated()访问属性值

Python版本是3.5.3


Tags: selfread属性valuepropertyminleftlight
1条回答
网友
1楼 · 发布于 2024-09-29 11:30:38

如果要向类而不是实例对象添加方法,只需通过LightSensor.set_calibration=set_calibration将其挂钩即可MethodType用于向类实例添加绑定方法

class LightSensor:
    def __init__(self):
        self.min_value = 1
        self.max_value = 2
        self.value_range = None
        self._reflected_light_intensity = 3

    def __repr__(self):
        return f"min: {self.min_value}, " \
               f"max: {self.max_value}, " \
               f"value_range: {self.value_range}, " \
               f"reflected_intensity: {self._reflected_light_intensity}"

def set_calibration(self,min_value,max_value):
    self.min_value = min_value
    self.max_value = max_value
    self.value_range = max_value - min_value

def read_calibrated(self):
    value = self._reflected_light_intensity
    print(value)
    return 100 * ( value - self.min_value ) / self.value_range

LightSensor.set_calibration = set_calibration
LightSensor.read_calibrated = read_calibrated

a = LightSensor()
a.set_calibration(20,60)
print (a)
print (a.read_calibrated())

b = LightSensor()

print (b)

结果:

min: 20, max: 60, value_range: 40, reflected_intensity: 3
3
-42.5
min: 1, max: 2, value_range: None, reflected_intensity: 3

相关问题 更多 >