Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

python - How do you change the value of one attribute by changing the value of another? (dependent attributes)

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

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

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Define darkness as a property

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

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

    def __str__(self):
        return '{},{}'.format(self.light, self.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.

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...