简体   繁体   English

Python Class 按子类更新父变量

[英]Python Class update parent variables by subclass

With my limited understanding of @property , @setter , and @getter , I came up with following code.由于我对@property@setter@getter的了解有限,我想出了以下代码。

class BitCounts:
    sign_bit = 0
    exponent_bits = 0
    mantissa_bits = 0
    _total_bits = 0

    @property
    def total_bits(self):
        return self._total_bits

    @total_bits.setter
    def total_bits(self):
        self._total_bits = self.sign_bit + self.exponent_bits + self.mantissa_bits


class Single(BitCounts):
    sign_bit = 1
    offset = 0x7F
    exponent_bits = 8
    mantissa_bits = 23
    _total_bits = BitCounts.total_bits


class Double(BitCounts):
    sign_bit = 1
    offset = 0x400
    exponent_bits = 11
    mantissa_bits = 52
    _total_bits = BitCounts.total_bits

My intention is to use the subclass Single and Double in other functions as a set of options like so, for example:我的意图是在其他函数中使用子类SingleDouble作为一组选项,例如:

    def some_function(option=Single):
        print("exponent bit counts are: %d", option.exponent_bits)
        print("mantissa bit counts are: %d", option.mantissa_bits)
        print("total bit counts are: %d", option.total_bits)

I would like total_bits to be automatically recalculated using values from subclass Single or Double .我希望使用子类SingleDouble中的值自动重新计算total_bits

I am trying to avoid extra functions to perform the calculation at subclass level.我试图避免额外的函数在子类级别执行计算。

With above codes, by calling Single.total_bits , or Double.total_bits , I am only getting a message saying <property object at 0x000002258DF7CB30> , what did I do wrong, and how can I fix it?使用上述代码,通过调用Single.total_bitsDouble.total_bits ,我只收到一条消息说<property object at 0x000002258DF7CB30> ,我做错了什么,我该如何解决?

The way you are using subclasses with hard-coded static values suggests these should be instances not subclasses.您使用具有硬编码 static 值的子类的方式表明这些应该是实例而不是子类。 This is also suggested by your temptation to use self even though you haven't made any instances.即使您没有创建任何实例,您也尝试使用self也暗示了这一点。 self refers to a particular instance. self指的是一个特定的实例。

Also, setters typically take a value as an argument.此外,setter 通常将值作为参数。 You don't have that in your setter because total_bits is completely dependent on other values.您的设置器中没有它,因为 total_bits 完全依赖于其他值。 As such you should just move your setter calculation to the getter and return the result of the calculation.因此,您应该将 setter 计算移至 getter 并返回计算结果。

Consider:考虑:

class BitCounts:
    def __init__(self, sign,offset, exponent, mantissa):       
        self.sign_bit = sign
        self.offset = offset
        self.exponent_bits = exponent
        self.mantissa_bits = mantissa

    @property
    def total_bits(self):
        return self.sign_bit + self.exponent_bits + self.mantissa_bits

# now make two instances:
single = BitCounts(1, 0x7F, 8, 23 )
double = BitCounts(1, 0x400, 11, 52)

print(single.total_bits)
# 32

print(double.total_bits)
# 64

You can use:您可以使用:

class Single(BitCounts):
    sign_bit = 1
    offset = 0x7F
    exponent_bits = 8
    mantissa_bits = 23
    _total_bits = BitCounts.total_bits

    def get_total_bits(self):
        # Update values here, example below
        self._total_bits = self._total_bits + 1
        return self._total_bits

Then call:然后调用:

option = Single()
option.get_total_bits()

The problem here is that you are trying to call an instance method from a class.这里的问题是您试图从 class 调用实例方法。

class A:
  @property
  def foo(self):
    return 1

print(A.foo) # prints an object of function "foo"
print(A().foo) # prints "1"

To accomplish this, at least from my knowledge you need to use a metaclass similar to what they do here: Per-class @property decorator in Python要做到这一点,至少据我所知,您需要使用类似于他们在这里所做的元类: Python 中的 Per-class @property decorator

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM