call函数
使用call方法实现1100的累加和
class Person:
def init(self):
pass
def __call__(self,num):
he=0
for i in range(1,num+1):
he+=i
print(he)
person=Person()
person(100)
运行截图:

本文介绍了一种使用Python的call方法实现1到100的累加和的方法。通过定义一个Person类,并在该类中使用__call__方法,可以将一系列数字相加并输出结果。
call函数
使用call方法实现1100的累加和
class Person:
def init(self):
pass
def __call__(self,num):
he=0
for i in range(1,num+1):
he+=i
print(he)
person=Person()
person(100)
运行截图:

2万+
617

被折叠的 条评论
为什么被折叠?