类型错误:“模块"对象不可调用 [英] TypeError: 'module' object is not callable

查看:67
本文介绍了类型错误:“模块"对象不可调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件C:\Users\Administrator\Documents\Mibot\oops\blinkserv.py",第 82 行,在 __init__ 中self.serv = 套接字(AF_INET,SOCK_STREAM)类型错误:模块"对象不可调用

为什么我会收到这个错误?我很困惑.

你需要知道什么才能回答我的问题?

解决方案

socket 是一个模块,包含类 socket.

你需要做socket.socket(...)from socket import socket:

<预><代码>>>>进口插座>>>插座<来自'C:\Python27\lib\socket.pyc'的模块'socket'>>>>套接字.socket<class 'socket._socketobject'>>>>>>>从套接字导入套接字>>>插座<class 'socket._socketobject'>

这是错误消息的含义:
它说 module object is not callable,因为您的代码正在调用 module 对象.模块对象是您在导入模块时获得的事物类型.您试图做的是在模块对象中调用一个 class 对象,该对象恰好与包含它的模块具有相同的名称.

这里有一种方法可以从逻辑上分解这种错误:

  • "模块对象不可调用.Python 告诉我我的代码试图调用无法调用的东西.我的代码试图调用什么?"
  • 代码试图调用 socket.那应该是可调用的!变量 socket 是我认为的吗?`
  • 我应该打印出什么是套接字并检查print socket

File "C:\Users\Administrator\Documents\Mibot\oops\blinkserv.py", line 82, in __init__
    self.serv = socket(AF_INET,SOCK_STREAM)
TypeError: 'module' object is not callable

Why am I getting this error? I'm confused.

What do you need to know to answer my question?

解决方案

socket is a module, containing the class socket.

You need to do socket.socket(...) or from socket import socket:

>>> import socket
>>> socket
<module 'socket' from 'C:\Python27\lib\socket.pyc'>
>>> socket.socket
<class 'socket._socketobject'>
>>>
>>> from socket import socket
>>> socket
<class 'socket._socketobject'>

This is what the error message means:
It says module object is not callable, because your code is calling a module object. A module object is the type of thing you get when you import a module. What you were trying to do is to call a class object within the module object that happens to have the same name as the module that contains it.

Here is a way to logically break down this sort of error:

  • "module object is not callable. Python is telling me my code trying to call something that cannot be called. What is my code trying to call?"
  • "The code is trying to call on socket. That should be callable! Is the variable socket is what I think it is?`
  • I should print out what socket is and check print socket

这篇关于类型错误:“模块"对象不可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆