|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
class Nstr:
def __init__(self, arg=''):
if isinstance(arg, str):
self.total = 0
for each in arg:
self.total += ord(each)
else:
print("参数错误!")
def __add__(self, other):
return self.total + other.total
def __sub__(self, other):
return self.total - other.total
def __mul__(self, other):
return self.total * other.total
def __truediv__(self, other):
return self.total / other.total
def __floordiv__(self, other):
return self.total // other.total
class Nstr:
def __init__(self, arg=''):
if isinstance(arg, str):
self.total = 0
for each in arg:
self.total += ord(each)
请问这个arg = '' 是什么意思?
这就像 print() 的默认参数 end = '\n' 一样,你这里定义函数 arg 默认等于 空字符串
所以调用时候可以不用传参数,因为有默认值,如果你没有设置默认值那么直接调用会报错,必须传入赋值给 arg
|
|