700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > TypeError: 'module' object is not callable 原因分析(python模块导入注意事项)

TypeError: 'module' object is not callable 原因分析(python模块导入注意事项)

时间:2018-06-02 02:34:02

相关推荐

TypeError: 'module' object is not callable 原因分析(python模块导入注意事项)

程序代码

class Person:

#constructor

def __init__(self,name,sex):

self.Name = name

self.Sex = sex

def ToString(self):

return 'Name:'+self.Name+',Sex:'+self.Sex

在IDLE中报错:

>>> import Person

>>> per = Person('dnawo','man')

Traceback (most recent call last):

File "<pyshell#2>", line 1, in <module>

per = Person('dnawo','man')

TypeError: 'module' object is not callable

原因分析:

Python导入模块的方法有两种:import module 和 from module import,区别是前者所有导入的东西使用时需加上模块名的限定,而后者不要。

正确的代码:

>>> import Person

>>> person = Person.Person('dnawo','man')

>>> print person.Name

>>> from Person import *

>>> person = Person('dnawo','man')

>>> print person.Name

原文地址:/bsndhswd/item/bffa62db1ceb393f48e1ddf6

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。