gpt4 book ai didi

Python 属性和变量赋值

转载 作者:行者123 更新时间:2023-12-05 06:19:07 24 4
gpt4 key购买 nike

下面是来自 this tutorial 的一个类旨在演示 Python 中属性的功能。

该类设置和获取摄氏温度,并转换为华氏温度:

class Celsius:
def __init__(self, current_temp = 25):
self.temperature = current_temp

def to_fahrenheit(self):
return (self.temperature * 1.8) + 32

def get_temperature(self):
print("Getting value")
return self._temperature

def set_temperature(self, value):
if value < -273:
raise ValueError("Temperature below -273 is not possible")
print("Setting value")
self._temperature = 50

temperature = property(get_temperature,set_temperature)

问题:self.temperature(一个实例变量)是否真的引用了类变量 温度?如果是,那为什么?

例子:

obj = Celsius()
print(obj._temperature, obj.temperature)

返回:

Setting valueGetting value50 50

我只是不明白具有指定值 (current_temp) 的实例变量如何实际引用类变量。对不起,如果我有什么错误。

最佳答案

Is it true that self.temperature (an instance variable) is actually referencing the class variable temperature?

你的意思是属性,不是变量。但是是的。

If so, then why?

因为类的实例没有temperature属性(它们只有_temperature属性,所以在Python找不到之后实例中的属性,它也会检查类。

也许您被抛弃了,因为 __init__ 方法分配给了 self.temperature。但是,这不会在实例上创建 temperature 属性,因为首先找到该属性。

complicated rules属性查找的工作原理;它们旨在让您可以做您想做的事 - 例如,让 property 可以工作。

I just don't understand how an instance variable with an assigned value (current_temp) could be actually referencing a class variable.

我在你的代码中没有看到 current_temp,所以我无法帮助你。


代码令人困惑的部分原因是它以非常不标准的方式做事。 Normally , property 在它的装饰器形式中使用,看起来像这样:

class Celsius:
def __init__(self, current_temp = 25):
# set the underlying value directly; don't use the property
# although you *can*, but it's arguably clearer this way
self._temperature = current_temp

def to_fahrenheit(self):
return (self.temperature * 1.8) + 32

# the getter is given the name that the property should have.
@property
def temperature(self):
print("Getting value")
return self._temperature

# The setter is "attached" to the getter with this syntax
@temperature.setter
# It doesn't matter that this has the same name, because of how the
# internal attachment logic works.
def temperature(self, value):
if value < -273:
raise ValueError("Temperature below -273 is not possible")
print("Setting value")
self._temperature = value # good idea to actually use the value!

好处:可以说,更好的设计是具有单独的 celciusfahrenheit 属性,并且能够从摄氏度或华氏度值创建实例(使用 code>staticmethod 创建工厂方法)。看起来像:

class Temperature:
# Secret implementation detail: we store the value in celcius.
# We could have chosen fahrenheit instead, and do the conversions
# the other way around. Users should not call this directly.
def __init__(self, c):
# Actually, I lied. One good reason to use the property in
# the init method is to enforce bounds checking.
self.celcius = c

# We offer factory methods that let the user be explicit about
# how the initial value is provided.
@staticmethod
def in_celcius(c):
return Temperature(c)

@staticmethod
def in_fahrenheit(f):
return Temperature((f - 32) * 5/9)

# Now we define properties that let us get and set the value
# as either a celcius value or a fahrenheit one.
@property
def celcius(self):
return self._celcius

@celcius.setter
def celcius(self, c):
if c < -273:
raise ValueError("below absolute zero")
self._celcius = c

@property
def fahrenheit(self):
return (self._celcius * 9/5) + 32

@fahrenheit.setter
def fahrenheit(self, f):
# use the other property to reuse the bounds-checking logic.
self.celcius = (f - 32) * 5/9

关于Python 属性和变量赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60962190/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com