Discuss / Python / 为什么Callable()会抛出异常?? 百度没找到答案

为什么Callable()会抛出异常?? 百度没找到答案

Topic source

旭_317

#1 Created at ... [Delete] [Delete and Lock User]

class Student(object): def init(self, name): self.name = name

def __call__(self, *args, **kwargs):
    print('My name is %s.' % self.name)

s = Student('zhangsan') print(Callable(s))

D:\Anaconda3\python.exe E:/ProgramDesign/PyProgram/Test/main.py Traceback (most recent call last): File "E:/Program_Design/Py_Program/Test/main.py", line 14, in <module> print(Callable(s)) File "D:\Anaconda3\lib\typing.py", line 1265, in __new raise TypeError("Type Callable cannot be instantiated; " TypeError: Type Callable cannot be instantiated; use a non-abstract subclass instead

Process finished with exit code 1

你代码是不是写错了 init写成了init callable也不能大写的 我复制代码改好了没问题 其它错误感觉跟这段代码没关系的

原来__init__打不出来。。
__call__()是能够让我们直接进行实例调用,即:
s('name')
而不是Callable(s)这样的代码,

当然可以写Callable('string'),这里参数不应该是你定义的实例s。
不过这个就和__call__()没有关系了。

楼主可能混淆了“实例()” 和 “类()”

上面我说的不对。。。 看到大写字母开头,我直接以为Callable那是个类名了。。。。。。。后面的allable直接被我忽略了。。。 好尴尬。。。


  • 1

Reply