程序代碼
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 html
=============下面這個解法更能體現問題所在,就是模塊的路徑問題 shell
問題: 函數
我不能肯定我爲何獲得這個錯誤: ui
源代碼以下: url
答覆: spa
"TypeError: 'module' object is not callable"這個信息是說你試圖把"urlparse"這個模塊做爲一個函數來調用,但它卻沒法調用。 htm
urlparse這個模塊包含urlparse 和 urlsplit等函數。我把urlsplit也拖了進來,它的名字和模塊名不一樣。這個可能能幫助你發現問題。如下是調用它們的兩種方法。 it
(1) ast
(2) class
方法1可能更適合你。