“AttributeError:'module'对象没有属性”数组

2024-09-29 21:44:24 发布

您现在位置:Python中文网/ 问答频道 /正文

这应该是一个垒球问题。在

我有一个名为Table的自定义类,其方法包括getTableName

class table(object):
    def __init__(self, tableName, aliasName = 'none'):
        """Create assign a name"""
        self.tableName = tableName
        if aliasName == 'none':
            self.aliasName = tableName
        else:
            self.aliasName = aliasName
        self.columns = []
        self.relatedTables = []
    def getTableName (self):
        return self.tableName

在一个单独的脚本中,我创建了一个表数组

^{pr2}$

然后追加tables数组。在

def appendTables(newTable):
    #check list of tables for match

    #if first batch of tables just append to tables
    if len(tables) == 0:
        tables.append(newTable)
        return
    found = False
    for oTable in tables:
        print "type newTable"  #type newTable
        print type(newTable)   #<class 'table.table'>
        print "type oTable"    #type oTable
        print type (oTable)    #<type 'module'>
        a = newTable.getTableName()  #OK
        b = oTable.getTableName()   #CRASH "AttributeError: 'module' object has no attribute 'getTableName'"

为什么Python不能识别这个类?在


Tags: selfnonetablesifobjectdeftypetable
1条回答
网友
1楼 · 发布于 2024-09-29 21:44:24

如果类table在文件table.py中定义,则需要

from table import table

或者使用table.table作为类名。另外,根据惯例,用户定义的类名应该以大写字母开头,这样您的代码看起来像

^{pr2}$

以及

from table import Table

以及

table.Table

相关问题 更多 >

    热门问题