如何跳过基于类实例属性的方法的执行?



在这个程序中,我应该将它存储在与另一个文件(main.py)相同的位置,并运行该文件来测试代码。

当我关掉电视的电源时,我遇到了一个问题,音量和频道不应该改变,但是它们却改变了。

一个'if'语句应该去某个地方,也许在幂函数?但我就是不知道该怎么做。

class Television:
min_volume = 0
max_volume = 2
min_channel = 0
max_channel = 3
def __init__(self):
self.status = False
self.muted = False
self.volume = Television.min_volume
self.channel = Television.min_channel
def power(self):
self.status = not self.status

def mute(self):
self.muted = not self.muted
def channel_up(self):
self.channel += 1
if self.channel > Television.max_channel:
self.channel = Television.min_channel
def channel_down(self):
self.channel -= 1
if self.channel < Television.min_channel:
self.channel = Television.max_channel
def volume_up(self):
self.volume += 1
if self.volume > Television.max_volume:
self.volume = Television.max_volume
def volume_down(self):
self.volume -= 1
if self.volume < Television.min_volume:
self.volume = Television.min_volume
def __str__(self):
Power = "True" if self.status else "False"
return f"TV Status: Power = {self.status}, Channel = {self.channel}, Volume = {self.volume}"

创建一个执行检查的包装器,这样您就不必为每个函数操心了。

class Television:
min_volume = 0
max_volume = 2
min_channel = 0
max_channel = 3
def power_check(func):
def wrapper(self, *args, **kwargs):
if not self.status:
print("Powered off")
return
print("Powered on")
return func(self, *args, **kwargs)
return wrapper
def __init__(self):
self.status = False
self.muted = False
self.volume = Television.min_volume
self.channel = Television.min_channel
def power(self):
self.status = not self.status
@power_check
def mute(self):
self.muted = not self.muted
@power_check
def channel_up(self):
self.channel += 1
if self.channel > Television.max_channel:
self.channel = Television.min_channel
@power_check
def channel_down(self):
self.channel -= 1
if self.channel < Television.min_channel:
self.channel = Television.max_channel
@power_check
def volume_up(self):
self.volume += 1
if self.volume > Television.max_volume:
self.volume = Television.max_volume
foo = Television()
foo.channel_up()
foo.power()
foo.channel_up()

的结果应该是:

Powered off
Powered on

您可以为每个需要功能的方法使用包装器。这个包装器使方法在电视打开时执行,而在电视关闭时不执行任何操作。

在你的类定义之前添加这个函数,并将它作为装饰器添加到你只希望在电视通电时执行的每个类方法中:

def requires_power(func):
"""Executes method if Televison.status = True and does nothing if it is False."""
def wrap(*args, **kwargs):
tv = args[0]
if not tv.status:
return None
else:
return func(*args, **kwargs)
return wrap

class Television:
min_volume = 0
max_volume = 2
min_channel = 0
max_channel = 3
def __init__(self):
self.status = False
self.muted = False
self.volume = Television.min_volume
self.channel = Television.min_channel
def power(self):
self.status = not self.status
@requires_power
def mute(self):
self.muted = not self.muted
@requires_power
def channel_up(self):
self.channel += 1
if self.channel > Television.max_channel:
self.channel = Television.min_channel
@requires_power
def channel_down(self):
self.channel -= 1
if self.channel < Television.min_channel:
self.channel = Television.max_channel
@requires_power
def volume_up(self):
self.volume += 1
if self.volume > Television.max_volume:
self.volume = Television.max_volume
@requires_power
def volume_down(self):
self.volume -= 1
if self.volume < Television.min_volume:
self.volume = Television.min_volume
def __str__(self):
return f"TV Status: Power = {self.status}, Channel = {self.channel}, Volume = {self.volume}"

测试:

tv = Television()
print(tv)
tv.power()
print(tv)
tv.volume_up()
print(tv)
tv.power()
tv.volume_up()
print(tv)
TV Status: Power = False, Channel = 0, Volume = 0
TV Status: Power = True, Channel = 0, Volume = 0
TV Status: Power = True, Channel = 0, Volume = 1
TV Status: Power = False, Channel = 0, Volume = 1

最新更新