每当我做OOP时,"超出最大递归深度"

BBe*_*dit 2 python oop recursion python-2.7

  File "C:\tv.py", line 20, in set_channel
    self.channel = new_channel
RuntimeError: maximum recursion depth exceeded
Run Code Online (Sandbox Code Playgroud)

我正在尝试我的第一个OOP问题.我的教科书中的问题是:

"通过将电视创建为对象来编写模拟电视的程序.允许用户增大/减小音量,并设置频道编号.确保频道和音量值保持在有效范围内."

我的问题是:

  1. 我的结构大致正确吗?(因为我选择制作inc/dec音量的方法等)

  2. 为什么我会收到递归错误?我想这意味着我处于某种无限循环中,但我绝对看不到它.

我未完成的"解决方案":

# Television with Classes
# User able to enter a channel number
# and increase/decrease the volume

class Television(object):
    """A TV set."""
    def __init__(self, channel = 1, volume = 20):
        self.channel = channel
        self.volume = volume

    def get_channel(self):
        """Return current channel."""
        return self.channel

    def set_channel(self, new_channel):
        """Set the channel."""
        if 1 < new_channel > 5:
            print "Invalid Channel. Must be between 1-5."
        else:
            self.channel = new_channel

    channel = property(get_channel, set_channel)

    def show_volume(self):
        """Return current volume."""
        return self.volume

    def inc_volume(self):
        """Increase the volume by 1 unit."""
        if self.volume >= 20:
            print "Volume is at maximum: ", get_volume, "\n"
        else:
            self.volume += 1

    def dec_volume(self):
        """Decrease the volume by 1 unit."""
        if self.volume <= 0:
            print "Volume is at minimum: ", get_volume, "\n"
        else:
            self.volume -= 1


sony = Television()
print "Current channel: ", sony.channel
print "Changing channel to 3..."
sony.channel(3)
print "Current channel: ", sony.channel
print "Current volume: ", self.volume
print "Increasing volume..."
sony.inc_volume()
print "Current volume: ", self.volume

raw_input("\n\nPress enter to exit.")
Run Code Online (Sandbox Code Playgroud)

Tha*_*e90 6

您的属性与您尝试使用该属性访问的值具有相同的名称.因此,当您访问它时,它会递归地尝试获取它自己的值.

重命名属性应该解决它.

将代码更改为:

channel = 0
Channel = property(get_channel, set_channel)
Run Code Online (Sandbox Code Playgroud)

将阻止递归发生.你将拥有Channel属性(所以调用它),它使用channelget/set_channel方法访问该字段.

对此属性的分配也不起作用sony.channel(3).您必须像任何其他值一样实际分配给该属性:

sony.channel = 3
Run Code Online (Sandbox Code Playgroud)

或(如果您已经更改了财产问题)

sony.Channel = 3
Run Code Online (Sandbox Code Playgroud)

但是,这不会阻止您稍后在卷上遇到的问题.self.volume未被声明为字段(或作为该属性的属性),并且get_volume在inc/dec_volume函数中也是如此.