Linked Questions

Popular Questions

How do I expose imported classes outside of the class

Asked by At

I have different classes that represent records in a database

For example

# my_records.py:

class TableARecord:
    field1: str
    field2: str

class TableBRecord:
    field1: str
    field2: str

I have another class that represents the entire DB. I want to give access to the record objects as well without importing them directly, and so I have this

# my_db.py:

from my_records import TableARecord, TableBRecord

class DB:
    TableARecord = TableARecord
    TableBRecord = TableBRecord

    def some_functionality():
        ...

    ...

And so I can use this syntax:

a_record = DB.TableARecord()

And this works well.

My question is specifically about these lines:

TableARecord = TableARecord
TableBRecord = TableBRecord

Is there a better or more pythonic way to "expose" these classes to the outside?

Related Questions