木子屋 Dnawo's BLOG

TypeError: 'module' object is not callable

👤 dnawo 📅 2009-07-27 👁 6835 👍 0 💬 0 🔄 来源:本站原创
Person.py模块内容如下:

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')

[color=Red]Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    per = Person('dnawo','man')
TypeError: 'module' object is not callable[/color]

原因分析:

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

评论(0)

暂无评论。

验证码
评论需审核通过后显示
← 上一篇 Python类实例属性和类属性分析 下一篇 → PythonWin 2.6.2编写Python脚本无法保存解决方…