How do I debug this AttributeError: 'int' object has no attribute 'increase'?
How do I debug this AttributeError: 'int' object has no attribute 'increase'?
我正在编写一些代码来练习,但我不断收到此错误:
File "...", line 12, in give_raise
self.increase = increase
AttributeError: 'int' object has no attribute 'increase'
这是我的代码:
class Employee:
"""Sort of simulates an employee."""
def __init__(self, first, last, salary):
"""Initialize attributes."""
self.first = first
self.last = last
self.salary = salary
def give_raise(self, increase = 5000):
"""gives raise"""
self.increase = increase
self.salary = self.salary + self.increase
Employee("first", "last", 30000)
Employee.give_raise(890)
print(Employee.salary)
请记住,我只是一个初学者。
谢谢您阅读此篇。我希望你能弄清楚哪里出了问题。
你可能是这个意思:
some_dude = Employee("first", "last", 30000)
some_dude.give_raise(890)
print(some_dude.salary)
您的代码正在做的是调用 give_raise
,就好像它是一个普通函数,像这样:
Employee.give_raise(self=890)
因此,如果您将此函数从 class 中分离出来并像 give_raise(890)
那样调用它,它会产生完全相同的效果。现在 self
是一个整数...
您可以使用 the official Python tutorial 了解 classes 的工作原理。
我正在编写一些代码来练习,但我不断收到此错误:
File "...", line 12, in give_raise
self.increase = increase
AttributeError: 'int' object has no attribute 'increase'
这是我的代码:
class Employee:
"""Sort of simulates an employee."""
def __init__(self, first, last, salary):
"""Initialize attributes."""
self.first = first
self.last = last
self.salary = salary
def give_raise(self, increase = 5000):
"""gives raise"""
self.increase = increase
self.salary = self.salary + self.increase
Employee("first", "last", 30000)
Employee.give_raise(890)
print(Employee.salary)
请记住,我只是一个初学者。
谢谢您阅读此篇。我希望你能弄清楚哪里出了问题。
你可能是这个意思:
some_dude = Employee("first", "last", 30000)
some_dude.give_raise(890)
print(some_dude.salary)
您的代码正在做的是调用 give_raise
,就好像它是一个普通函数,像这样:
Employee.give_raise(self=890)
因此,如果您将此函数从 class 中分离出来并像 give_raise(890)
那样调用它,它会产生完全相同的效果。现在 self
是一个整数...
您可以使用 the official Python tutorial 了解 classes 的工作原理。