本文介绍了在Python Class对象,如何自动更新属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有有相关多个属性的一类,例如:

 类SomeClass的:
    高清__init __(个体经营中,n = 0):
        self.list =范围(N)
        self.listsquare = [X ** 2在self.a X]

如果我做一个对象通常那会没问题的,以

  A = SomeClass的(10)

我会得到2列出, a.list a.listsquare

现在,如果我想要一个空的对象,第一,和一个属性分配给它,我想其他的属性自动更新,例如,如果我这样做

  B = SomeClass的()
b.list =范围(5,10)

我想 b.listsquare 自动更新,并且还反过来(指定 b.listsquare 和自动更新 b.list )。这可能吗?是的的这个正确的选择?


感谢你的一切,但我所有的不同的答案完全压倒。谁能给一个完整的解决方案,所以我可以学习写我自己?

我想实现一个类有3个属性长度列表 listsquare 这样:


  1. 如果我 A =美孚(3),我得到则为a.length = 3 a.list = [0,1,2] a.listsquare = [0,1,4]

  2. 如果我 B =美孚()。列表= [5,6] ,我得到 b.length个= 2 b.listsquare = [25,36]

  3. 如果我 C =美孚()。listsquare = [4,9] ,我得到 c.length = 2 c.list = [2,3]


解决方案

如果更新由于对其他属性的更新一个属性就是你要找的内容(而不是重新计算对获得下游属性的值)的使用属性setter方法​​:

 类SomeClass的(对象):
    高清__init __(自我,N):
        self.list =范围(0,n)的    @属性
    DEF列表(个体经营):
        回报self._list
    @ list.setter
    DEF列表(自我,VAL):
        self._list = VAL
        self._listsquare = [X ** 2在self._list X]    @属性
    高清listsquare(个体经营):
        回报self._listsquare
    @ listsquare.setter
    高清listsquare(个体经营,VAL):
        self.list = [INT(POW(X,0.5))对于x位于Val]>>> C = SomeClass的(5)
>>> c.listsquare
[0,1,4,9,16]
>>> c.list
[0,1,2,3,4]
>>> c.list =范围(0,6)
>>> c.list
[0,1,2,3,4,5]
>>> c.listsquare
[0,1,4,9,16,25]
>>> c.listsquare = [X ** 2的范围X(0,10)
>>> c.list
[0,1,2,3,4,5,6,7,8,9]

I have a class which has multiple attributes that are related, for example:

class SomeClass:
    def __init__(self, n=0):
        self.list = range(n)
        self.listsquare = [ x**2 for x in self.a ]

If I make an object normally that would no problem, with

a = SomeClass(10)

I will get 2 lists, a.list and a.listsquare.

Now if I want to make a empty object first, and assign one attribute to it, I want the other attributes to be automatically updated, for example if I do

b = SomeClass()
b.list = range(5,10)

I want b.listsquare to be automatically updated, and also the other way around (assign b.listsquare and auto update b.list). Is this possible? Is Class the right choice for this?


Thanks to you all, but I'm completely overwhelmed by all the different answers. Can anyone give a complete solution so I can learn write my own?

I would like to achieve a class Foo with 3 attributes length, list and listsquare such that:

  1. If I do a = Foo(3), I get a.length = 3, a.list = [0, 1, 2], a.listsquare = [0, 1, 4].
  2. If I do b = Foo().list = [5, 6], I get b.length = 2, b.listsquare = [25, 36].
  3. If I do c = Foo().listsquare = [4, 9], I get c.length = 2, c.list = [2, 3].
解决方案

if updating one property due to an update on another property is what you're looking for (instead of recomputing the value of the downstream property on access) use property setters:

class SomeClass(object):
    def __init__(self, n):
        self.list = range(0, n)

    @property
    def list(self):
        return self._list
    @list.setter
    def list(self, val):
        self._list = val
        self._listsquare = [x**2 for x in self._list ]

    @property
    def listsquare(self):
        return self._listsquare
    @listsquare.setter
    def listsquare(self, val):
        self.list = [int(pow(x, 0.5)) for x in val]

>>> c = SomeClass(5)
>>> c.listsquare
[0, 1, 4, 9, 16]
>>> c.list
[0, 1, 2, 3, 4]
>>> c.list = range(0,6)
>>> c.list
[0, 1, 2, 3, 4, 5]
>>> c.listsquare
[0, 1, 4, 9, 16, 25]
>>> c.listsquare = [x**2 for x in range(0,10)]
>>> c.list
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

这篇关于在Python Class对象,如何自动更新属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 11:16