#author("2023-09-10T12:20:50+08:00","default:Admin","Admin") #author("2023-09-10T12:22:42+08:00","default:Admin","Admin") [[Python]] &color(red){※This article is based on Python 3.7.3}; #contents *语法错误 [#q278a388] ** TypeError: 'str' object is not callable [#ucd31985] str( )是python自带函数,是python保留的关键字,定义变量时应该避免使用str作为变量名 如果在使用str( )函数之前已经定义过str变量,则会出现TypeError: ‘str’ object is not callable这个报错 ** UnboundLocalError: local variable 'xxx' referenced before assignment [#k56c9c0b] 在函数外部已经定义了变量n,在函数内部对该变量进行运算,运行时会遇到了这样的错误:主要是因为没有让解释器清楚变量是全局变量还是局部变量。有时候在其他模块声明该变量,在本模块使用时也会出现。 那么问题就来了,出现这个问题我们就要考虑程序是按照全局变量,就是经过函数运算到函数外面还生效,还是按照局部变量在函数外生效。 第一种,当全局变量来看,就是使用global关键字,在函数内部先声明a这个变量是全局变量。 #codeprettify{{ def func(): '''声明''' global a; '''主体''' ... ... }} ** TypeError: '>' not supported between instances of 'str' and 'int' [#u3de0626] 报错原因:字符串(str)未转化便和整数(int)进行比较 解决办法:转换即可 #codeprettify{{ num = int(input("请输入数字:")) if num > 10: print(">") else: print("<") }} ** takes 1 positional argument but 2 were given [#c0fdcf65] 某某函数设置一个变量,但是你给了两个但是你给了两个参数 *** 第一种 [#s8810a8e] #codeprettify{{ def fun(x): return x fun(1,2) }} *** 第二种 [#e660fe5f] #codeprettify{{ class MyProperty: def __init__(self,fset=None,fget=None,fdel=None): self.fset = fset self.fget = fget self.fdel = fdel def __get__(self, instance, owner): return self.fget(instance) def __set__(self, instance, value): self.fset(instance,value) def __delete__(self, instance): self.fdel(instance) class my: def __init__(self): self._x = None def get(self): return self._x def set(self,value): self._x = value def delX(self): del self._x x = MyProperty(get,set,delX) c = my() c.x='x_man' print(c.x) print(c._x) ''' D:\Python\python.exe D:/pycharm/code/小甲鱼/11-27/自定义property.py Traceback (most recent call last): File "D:/pycharm/code/小甲鱼/11-27/自定义property.py", line 25, in <module> c.x='x_man' File "D:/pycharm/code/小甲鱼/11-27/自定义property.py", line 9, in __set__ self.fset(instance,value) TypeError: get() takes 1 positional argument but 2 were given Process finished with exit code 1 ''' }} *** 第三种 [#o7d92dee] 就是self参数忘记写了,会导致出现以上问题 * 环境错误 [#j9e1269a] ** SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xd7 in position 0: invalid continuation byte [#t5b80a24] 把文件的编码改为 UTF-8 ** SyntaxError: Non-UTF-8 code starting with '\xc9' in file xx.py [#gb7cb44c] SyntaxError: Non-UTF-8 code starting with '\xc9' in file ./test.py on line 5, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details 代码第一行加入: #codeprettify{{ # -*- coding: UTF-8 -*- }} ** ImportError: urllib3 v2.0 only supports OpenSSL 1.1.1+ [#tc8778dc] 下面两种错误 - ImportError: urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'OpenSSL 1.0.2k-fips 26 Jan 2017' - ImportError: urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'OpenSSL 1.1.0j 20 Nov 2018'. See: https://github.com/urllib3/urllib3/issues/2168 Terminal窗口执行 pip install urllib3==1.26.15 #hr(); コメント: #comment_kcaptcha