Python_Debug

Python_Debug

1. 类没有实例化造成 TypeError: XX() missing 1 required positional argument: 'XXX'

错误代码:

class Solution_1:
    def maxInWindows(self, num, size):
        if not num or not size:
            return []
        res = []
        for i in range(len(num) - size + 1):
            res.append(max(num[i:i + size]))
        return res


if __name__ == '__main__':
    print(Solution_1.maxInWindows([2, 3, 4, 2, 6, 2, 5, 1], 3)) #'Solution_1'没有带括号,没有被实例化

正确代码:

class Solution_1:
    def maxInWindows(self, num, size):
        if not num or not size:
            return []
        res = []
        for i in range(len(num) - size + 1):
            res.append(max(num[i:i + size]))
        return res


if __name__ == '__main__':
    print(Solution_1().maxInWindows([2, 3, 4, 2, 6, 2, 5, 1], 3)) # TODO 1.建立物件時,只需使用類名,且類名後面要帶括號!

問題Missing 1 required positional argument引出的關於python例項化的經驗教訓

2. Python中类后面带不带括号的差别

新旧类区别不在于带不带括号。而在于是否继承object吧。

类名称后面有无括号的区别

3. Python递归报错:RuntimeError: maximum recursion depth exceeded in comparison

原因:

Python中默认的最大递归深度是989,当尝试递归第990时便出现递归深度超限的错误:

RuntimeError: maximum recursion depth exceeded in comparison

解决方法:

可以手动设置递归调用深度:

>>> import sys
>>> sys.setrecursionlimit(10000000)

4. Python3.x 报错:AttributeError: module 'string' has no attribute 'lowercase'

在Leetcode的一道题遇到的问题,想获得26个小写英文字母,输的是string.lowercase,报错:

AttributeError: module 'string' has no attribute 'lowercase'

原因:

Python3.x的string库变动

解决办法

https://www.cnblogs.com/jonm/p/8448118.html

print([chr(i) for i in range(65, 91)])  # 所有大写字母
print([chr(i) for i in range(97, 123)])  # 所有小写字母
print([chr(i) for i in range(48, 58)])   # 所有数字

####################
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']


import string   # 导入string这个模块
print(string.digits)  # 输出包含数字0~9的字符串
print(string.ascii_letters)  # 包含所有字母(大写或小写)的字符串
print(string.ascii_lowercase)  # 包含所有小写字母的字符串
print(string.ascii_uppercase)  # 包含所有大写字母的字符串


##############
0123456789
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ

5. python3提示错误“ImportError: No module named ‘MySQLdb’”

https://blog.csdn.net/qq_769932247/article/details/88188945

问题描述:
项目在转到python3.7时,原先的导入MySQLdb模块都提示无法导入,pip install mysqldb也安装失败。
问题原因:
python2和python3在数据库模块支持这里存在区别,python2是mysqldb,而到了python3就变成mysqlclient,pip install mysqlclient即可。

赞赏
Nemo版权所有丨如未注明,均为原创丨本网站采用BY-NC-SA协议进行授权,转载请注明转自:https://nemo.cool/414.html

Nemo

文章作者

推荐文章

发表回复

textsms
account_circle
email

Python_Debug
1. 类没有实例化造成 TypeError: XX() missing 1 required positional argument: 'XXX' 错误代码: class Solution_1: def maxInWindows(self, num, size): if not num or …
扫描二维码继续阅读
2019-09-26