【问题标题】:how to export a variable value only based on half of a condition, but not the other?如何仅基于条件的一半导出变量值,而不是另一个?
【发布时间】:2022-01-09 19:04:05
【问题描述】:

我有这个练习,我需要减少一个类属性值(值总是> = 0),然后恢复到原始值。我尝试只计算“-= 1”的次数,但如果该方法继续运行,那么我无法获得正确的“计数”。请看代码:

# Tee ratkaisusi tähän:
class DecreasingCounter:
    def __init__(self, initial_value: int): 
        self.value = initial_value
    def print_value(self):
        print("value:", self.value)
    def decrease(self):
        global count
        count = 0
        if self.value > 0:
            self.value -= 1
            count += 1
        else: 
            self.value == 0 

    def set_to_zero(self):
        self.value -= self.value
        return True
    def reset_original_value(self):
        self.value += count

    # Write the rest of the methods here!
if __name__ == "__main__":
    counter = DecreasingCounter(5)
    counter.decrease()
    counter.decrease()
    counter.decrease()
    counter.print_value()
    counter.reset_original_value()
    counter.print_value()

正如您在第三次运行 counter.decrease() 时看到的那样,我的 count 回到“0”。

如何导出权限count

【问题讨论】:

  • 也许你应该有一个initial_value 和一个current_value 成员变量。
  • 有一个更简单的方法。当您可以轻松撕毁initial_value 时,为什么还要跟踪count?同样对于set_to_zero,为什么不只是self.value = 0

标签: python class global-variables


【解决方案1】:

你已经很接近了!但是看看你的计数会发生什么:

    def decrease(self):
        global count
        count = 0 # Here!

你一开始将count设置为0,所以count总是0或1。你要做的就是把它移到课堂外:

count = 0
class DecreasingCounter:
    ...

但现在你需要记住重置count

    def reset_original_value(self):
        global count
        self.value += count
        count = 0

但如果它是一个全局变量是有原因的呢?如果没有,您可以为此使用实例属性:

class DecreasingCounter:
    def __init__(self, initial_value: int):
        self.value = initial_value
        self.count = 0
        # or instead of counting calls, use the current_value directly
        self.current_value = initial_value

【讨论】:

    【解决方案2】:

    我认为只显式维护当前值和初始值会更简单:

    class DecreasingCounter:
        def __init__(self, initial_value: int): 
            self.initial_value = initial_value
            self.value = initial_value
    
        def print_value(self):
            print("value:", self.value)
    
        def decrease(self):
            if self.value > 0:
                self.value -= 1
    
        def set_to_zero(self):
            self.value = 0
    
        def reset_original_value(self):
            self.value = self.initial_value
    

    现在你可以这样做了:

    if __name__ == "__main__":
        counter = DecreasingCounter(5)
    
        counter.decrease()
        counter.decrease()
        counter.decrease()
        counter.print_value()
    
        counter.reset_original_value()
        counter.print_value()
    
        counter.set_to_zero()
        counter.print_value()
    
        counter.decrease()
        counter.print_value()
    

    给予:

    value: 2
    value: 5
    value: 0
    value: 0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-01
      • 2021-12-31
      • 1970-01-01
      • 2018-06-15
      • 1970-01-01
      • 2019-03-01
      • 2022-11-23
      • 2023-01-20
      相关资源
      最近更新 更多