如何通过更改另一个属性的值来更改一个属性的值? (相关属性) [英] How do you change the value of one attribute by changing the value of another? (dependent attributes)

查看:105
本文介绍了如何通过更改另一个属性的值来更改一个属性的值? (相关属性)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我最近开始涉足OOP,到目前为止一切进展顺利.虽然我本身没有任何问题,但是我希望有一个令人惊奇的功能,尽管我找不到关于该功能的任何文档.

So I've recently dived into OOP, and so far everything is going smooth. Whilst I have no issues per se, there's an amazing feature which I hope exists, though I cannot find any documentation on said feature.

在为对象分配属性时,我经常发现我必须更改依赖于其他属性的属性,例如光明与黑暗.这是一个示例:

When assigning attributes to objects, I often find that I have to change attribues that are dependent upon others, say, light and darkness. Here's an example:

class shade:

    def __init__(self, light):
        self.light=light
        self.darkness=100-light

    def __str__(self):
        return (str(self.light) +  ',' + str(self.darkness))



>>> shade1=shade(30,70)
>>> shade1.light
30
>>> shade1.darkness
70

现在,尽管这很酷,但我希望发生的过程相同,但是如果一个属性发生更改,则在同一对象内.如果我重设了光的属性(是的),我希望黑暗相应地增加/减少.如果可以更改光的属性,并返回新的光/暗值,那么我可以使用一个函数来执行此操作,但是如果我只是通过重新分配其值来更改光的属性,则我想采用一种方法,没有使用功能,例如:

Now, while that is cool and all, what I would like is the same process to occur, but within the same object if a change in one attribute occurs. If I reset the property of light (oh yeah), I want darkness to incr/decr accordingly. I can do this with a function if it works to change the property of light, returning the new value of light/darkness, but I'd like a way to do this if i change the property of light simply by re-assigning it's value, w/out the use of functions, Like this:

>>> shade1.light=50
>>> shade1.light
50
>>> shade1.darkness
50

此外,作为OOP的新手,如果有人知道一些好的教程,那也将是巨大的帮助.

Also, as am new to OOP, if anyone knows some good tutorials, that would also be a massive help.

谢谢.

推荐答案

darkness定义为属性

class shade:
    def __init__(self, light):
        self.light=light

    @property
    def darkness(self):
        return 100 - self.light

    def __str__(self):
        return (str(self.light) +  ',' + str(self.darkness))

属性从外部显示为属性,但在内部充当函数调用.当您说s.darkness时,它将调用您为其属性提供的函数.这样一来,您只能在内部维护一个变量.

Properties outwardly appear as attributes, but internally act as function calls. When you say s.darkness it will call the function you've provided for its property. This allows you to only maintain one variable internally.

如果您希望能够通过分配给黑暗来对其进行修改,请为该属性添加setter

If you want to be able to modify it by assigning to darkness, add a setter for the property

class shade:
    def __init__(self, light):
        self.light=light

    @property
    def darkness(self):
        return 100 - self.light

    @darkness.setter
    def darkness(self, value):
        self.light = 100 - value

从而实际上修改光.如果您以前从未看过属性,我建议您在函数的主体中添加一些print()以便可以看到何时调用它们.

Thereby actually modifying light. If you've never seen properties before, I'd recommend throwing in some print()s to the bodies of the functions so you can see when they are called.

>>> s = shade(70)
>>> s.light
70
>>> s.darkness
30
>>> s.light = 10
>>> s.darkness
90
>>> s.darkness = 20
>>> s.light
80

这篇关于如何通过更改另一个属性的值来更改一个属性的值? (相关属性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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