2

This should be a softball question.

I have a custom class called Table with methods including 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

In a separate script I created an array of tables

import table
tables = []

Then I append the tables array.

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

Why does Python not recognize the class?

1
  • I think this conflicts with the module name. What is this module/package called ? Try using a different local variable. Example: for t in tables: Commented Aug 19, 2014 at 14:48

1 Answer 1

2

If the class table is defined in the file table.py, you need either

from table import table

or use table.table for the name of the class. Also, user-defined class names should, by convention, start with a capital letter, so that your code would look like

class Table(object):

and

from table import Table

and

table.Table
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.