|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #-------返回字符串中字符的ASCII码和-----#
- class Nint(int):
- def __new__(cls, string):
-
- if type(string) == 'str':
-
-
- total = 0
- for i in string:
- total = total+ ord(i)
- string = total
- return int.__new__(cls, string)
-
复制代码
这样能输出值
但是一旦里面是字符就报错- >>> Nint('A')
- Traceback (most recent call last):
- File "<pyshell#59>", line 1, in <module>
- Nint('A')
- File "C:\Users\asus\Desktop\python练习\041\041-2-2.py", line 12, in __new__
- return int.__new__(cls, string)
- ValueError: invalid literal for int() with base 10: 'A'
- >>>
复制代码
我觉得应该是if判断那里出现问题了,求解答是怎么回事?
还有一个问题:
在小甲鱼41讲课后习题中,为什么__del__方法里还要del self.new_file?
- #------删除对象时文件自动关闭-----#
- class FileObject:
- def __init__(self, filename = 'record.txt'):
- self.new_file = open(filename, 'r+')
- def __del__(self):
- self.new_file.close()
- del self.new_file
-
复制代码 |
|