0

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
4

2 回答 2

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 中创建对象就像调用一个函数一样。

于 2013-05-02T02:54:35.433 回答
0

module意味着您只导入了 python 文件,但不一定是其中的任何内容。如果文件中有一个与 Python 文件同名的类,则必须执行from RetailItem import RetailItem. 对于初学者来说,这很奇怪。

于 2013-05-02T02:54:44.437 回答