p1+p2出错,说p1未定义

class Programer(object):


def __init__(self, name, age):

#super(Programer, self).__init__()

#self.arg = arg

self.name = name

if isinstance(age,int):

self.age = age

def __eq__(self, other):

if isinstance(other,Programer):

if self.age == other.age:

return True

else:

return False

else:

raise Exception('the type of object must be Programer')


def __add__(self, other):

if isinstance(other,Programer):

return self.age + other.age

else:

raise Exception('the type of object must be Programer')

if __name__=='__main__':

p1 = Programer('Yue', 20)

p2 = Programer('Wang', 24)

print (p1 == p2)

print (P1 + p2)

结果:

False    print (P1 + p2)

NameError: name 'P1' is not defined


dachuan
浏览 885回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python