运行问题TypeError:'module'对象不可调用我无法理解为什么会这样

时间:2013-05-02 02:51:34

标签: python

TypeError:'module'对象不可调用我无法理解为什么会发生这种情况请帮助我不确定当我从超类调用时它不读取它的子类就表示它没有被调用

import RetailItem
import CashRegister

def main():
    #info1 = ['Jacket', '12', '59.95']
    #info2 = ['Designer', '40', '34.95']
    #info3 = ['Shirt', '20', '24.95']

    print ('there are three items')
    info1 = str(input('whats the description'))
    info2 = str(input('whats the units'))
    info3 = str(input('whats the price'))

    info4 = str(input('whats the description'))
    info5 = str(input('whats the units'))
    info6 = str(input('whats the price'))

    info7 = str(input('whats the description'))
    info8 = str(input('whats the units'))
    info9 = str(input('whats the price'))


    first = CashRegister.RetailItem(info1,info2,info3)
    second = CashRegister.RetailItem(info4,info5,info6)
    third = CashRegister.RetailItem(info7,info8,info9)

    #first1 = CashRegister.CashRegister(info1[0],info1[1],info1[2])
    print ("Description       Units in Inventory          Price\n")


    #print(first.show_items())


    print (first.__str__())
    print (second.__str__())
    print (third.__str__())



#main function

2 个答案:

答案 0 :(得分:1)

错误消息告诉您RetailItem是一个模块,您不能像函数一样调用它,这就是您在这里所做的:

CashRegister.RetailItem(info1,info2,info3)

“将其称为函数”意味着您在名称后面写(...)。这是您的导入声明:

import RetailItem

所以你应该认识到RetailItem是一个模块。如果在RetailItem模块中定义了一个名为do_stuff()的函数,那么您可以这样调用它:

val = RetailItem.do_stuff()

另一方面,如果RetailItem是CashRegister模块中定义的函数,那么您所要做的就是:

import CashRegister


val = CashRegister.RetailItem(...)

RetailItem实际上看起来像是一个类,但在python中创建对象就像调用一个函数一样。

答案 1 :(得分:0)

module表示您只导入了python文件,但不一定是其中的任何内容。如果文件中的类名与Python文件的名称相同,则必须执行from RetailItem import RetailItem。对于初次参加者来说,这是一个奇怪的事情。