TypeError-Python中的类

TypeError-Python中的类,python,class,typeerror,Python,Class,Typeerror,我是Python的初学者,刚刚开始学习类。我相信这可能是一些非常基本的东西,但为什么这段代码: class Television(): def __init__(self): print('Welcome your TV.') self.volume = 10 self.channel = 1 def channel(self, channel): self.channel = input('Pick a chann

我是Python的初学者,刚刚开始学习类。我相信这可能是一些非常基本的东西,但为什么这段代码:

class Television():
    def __init__(self):
        print('Welcome your TV.')
        self.volume = 10
        self.channel = 1
    def channel(self, channel):
        self.channel = input('Pick a channel: ')
        print('You are on channel ' + self.channel)
    def volume_up(self, amount):
        self.amount = ('Increase the volume by: ')
        self.volume += self.amount
        print('The volume is now ' + self.volume)
    def volume_down(self, amount):
        self.amount = ('Decrease the volume by: ')
        self.volume -= self.amount
        print('The volume is now ' + self.volume)
myTele = Television()
myTele.channel()
myTele.volume_up()
myTele.volume_down()
产生以下错误:

TypeError: 'int' object is not callable, Line 18
编辑:我将代码更改为:

class Television():
    def __init__(self, volume = 10, channel = 1):
        print('Welcome your TV.')
        self.volume = volume
        self.channel = channel
    def change(self, channel):
        self.channel = input('Pick a channel: ')
        print('You are on channel ' + self.channel)
    def volume_up(self, amount):
        self.amount = int(input('Increase the volume by: '))
        self.volume += self.amount
        print('The volume is now ' + str(self.volume))
    def volume_down(self, amount):
        self.amount = int(input('Decrease the volume by: '))
        self.volume -= self.amount
        print('The volume is now ' + str(self.volume))
myTele = Television()
myTele.change()
myTele.volume_up()
myTele.volume_down()
但它的回报是:

TypeError: change() missing 1 required positional argument: 'channel'

同样,这是来自刚开始上课的人,所以如果我做了一些明显错误的事情,请不要太苛刻。谢谢。

您在
\uuuuu init\uuuu
中指定了一个
频道
属性:

self.channel = 1
这会在类上隐藏
channel()
方法。重命名属性或方法

实例上的属性胜过类上的属性(数据描述符除外;想想
property
s)。从:

类定义中定义的变量为类属性;它们由实例共享。实例属性可以在具有
self.name=value
的方法中设置。类和实例属性都可以通过符号“
self.name
”访问,并且实例属性在以这种方式访问时隐藏具有相同名称的类属性


您的方法还需要一个您在示例中没有传递的参数,但我想接下来您将自己解决这个问题。

您有一个名为
channel
的方法和一个名为
channel
的变量。该变量是一个
int
,它会隐藏该方法(窃取其名称以便您无法访问)。重命名方法或变量,这将解决您的问题。

实际上,在编辑暴露了Martijn已经指出的问题之后,您的代码就出现了问题。您的
self.change()
需要一个在
myTele.change()中未提供的参数。由于未在方法中使用参数,因此应将
change
定义为:

def change(self):
    self.channel = input('Pick a channel: ')
    print('You are on channel ' + self.channel)
volume\u up
volume\u down
实际上是将字符串分配给self.amount,而不是调用函数。你可能想把它们改成

def volume_up(self, amount):
    self.amount = input('Increase the volume by: ')
    self.volume += self.amount
    print('The volume is now ' + self.volume)
def volume_down(self, amount):
    self.amount = input('Decrease the volume by: ')
    self.volume -= self.amount
    print('The volume is now ' + self.volume)

由于每次使用前都要设置
self.amount
,因此您可以将其作为方法的局部变量(
amount
)。如果您将来计划有一个方法
xyz()
,该方法使用
self.amount
,而不首先设置它,那么您应该确保
self.amount
\uuuuu init\uuuuuuz()
中设置为合理的值,以防在卷更改方法之前调用
xyz()

Martijn,您有多好?我的意思是,每次我提出一个Python问题,你都是第一个受到打击的人:)@MorganWilde:你说得对。。我同意!!事实上,他的答案是很好的学习来源:)谢谢,你澄清了Martijn关于在方法中传递参数的说法。你很幸运,我在回答这个问题时迟到了,并注意到了你的编辑,如此晚的更改以及需要回答的其他问题往往没有引起注意。