现在很清楚元类是什么,有一个相关的概念,我一直在使用,而不知道它的真正含义.
我想每个人都用括号做错了,导致"对象不可调用"异常.更重要的是,使用__init__并__new__导致想知道这种血腥__call__可以用于什么.
你能给我一些解释,包括魔术方法的例子吗?
我是Python的新手并且遵循教程.list教程中有一个例子:
example = list('easyhoss')
Run Code Online (Sandbox Code Playgroud)
现在,在教程中,example= ['e','a',...,'s'].但在我的情况下,我得到以下错误:
>>> example = list('easyhoss')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
Run Code Online (Sandbox Code Playgroud)
请告诉我我错在哪里.我搜索了这个,但它是不同的.
我很好奇,3个不同的括号在python编程中意味着什么?我不确定这是否正确,但如果我错了请纠正我.
[] - # Normally used for dictionaries, list items
() - # Used to identify params
{} - # I have no idea what this does...
Run Code Online (Sandbox Code Playgroud)
或者,如果这些括号可用于其他目的,欢迎任何建议!谢谢!
我经常从我的 Python 代码中得到未捕获的异常(错误),这些异常被描述为TypeErrors. 经过大量的实验和研究,我收集了以下示例(以及细微的变化):
TypeError: func() takes 0 positional arguments but 1 was given
TypeError: func() takes from 1 to 2 positional arguments but 3 were given
TypeError: func() got an unexpected keyword argument 'arg'
TypeError: func() missing 1 required positional argument: 'arg'
TypeError: func() missing 1 required keyword-only argument: 'arg'
TypeError: func() got multiple values for argument 'arg'
TypeError: MyClass() takes no arguments
TypeError: unsupported operand type(s) for +: 'int' and 'str'
TypeError: can only concatenate str …Run Code Online (Sandbox Code Playgroud) 我找不到我面临的问题......
这正是错误告诉我的:
File "C:/Users/Rodrigo Villalta/Desktop/listasparimpar.py", line 38,
in listas_par_impar
if lista2(m) > lista2 [m+1]: TypeError: 'list' object is not callable
Run Code Online (Sandbox Code Playgroud)
这是代码:
def listas_par_impar(lista,lista2):
lista3=[]
lista4=[]
for i in lista2:
if i % 2 == 0:
lista=lista+[i]
else:
pass
for i in lista:
if i %2 != 0:
lista2=lista2+[i]
else:
pass
for i in lista:
if i%2==0:
if i not in lista3:
lista3=lista3+[i]
lista=lista[1:]
for i in lista2:
if i%2!=0:
if i not in lista4:
lista4=lista4+[i]
lista=lista[1:]
for recorrido in range(1,len(lista)): …Run Code Online (Sandbox Code Playgroud) 我有一个简单的脚本:
list = [1, 2, 3, 4, 5]
myrange = list(range(1, 10))
for number in list:
if number in myrange:
print(number, 'is between 1 and 10')
Run Code Online (Sandbox Code Playgroud)
但是,每当我尝试运行我的脚本时,Python都会引发错误:
Traceback (most recent call last):
File "python", line 2, in <module>
TypeError: 'list' object is not callable
Run Code Online (Sandbox Code Playgroud)
这个错误是什么意思?我为什么要这样做?我该如何解决?