我有下面的部分,它创建了几个人对象并在这些对象上应用了一些方法。
class Person:
def __init__(self, name, age, pay=0, job=None):
self.name = name
self.age = age
self.pay = pay
self.job = job
def lastname(self):
return self.name.split()[-1]
def giveraise(self,percent):
return self.pay *= (1.0 + percent)
if __name__ == '__main__':
bob = Person('Bob Smith', 40, 30000, 'software')
sue = Person('Sue Jones', 30, 40000, 'hardware')
people = [bob,sue]
print(bob.lastname())
print(sue.giveraise(.10))
一旦我运行这个程序,这就是输出——
语法错误:无效的语法
但是当我使用下面的代码运行时,我没有任何问题,
if __name__ == '__main__':
bob = Person('Bob Smith', 40, 30000, 'software')
sue = Person('Sue Jones', 30, 40000, 'hardware')
people = [bob,sue]
print(bob.lastname())
sue.giveraise(.10)
print(sue.pay)
两种情况有什么区别