import statement - package visibility problem

Kristóf Stróbl kristof at rubin.hu
Thu Apr 7 09:46:58 EDT 2005


Hi!

Laszlo Zsolt Nagy wrote:
> 
> I have the following structure:
> 
> 
> /Lib/Server/Db/
>    __init_.py
>    DatabaseConnection.py
>    Adapters/
>       __init__.py
>       FireBirdConnection.py
> 
> Important file contents are:
> 
> /Lib/Server/Db/__init__.py:
> 
>    import DatabaseConnection
>    import Adapters
> 
> /Lib/Server/Db/DatabaseConnection.py:
> 
>    class DatabaseConnection(object):
>          pass
> 
> 
> /Lib/Server/Db/Adapters/__init__.py
> 
>    import FireBirdConnection
> 
> /Lib/Server/Db/Adapters/FireBirdConnection.py:
> 
>    from DatabaseConnection import DatabaseConnection
>    class FireBirdConnection(DatabaseConnection):
>           pass
> 
> ...
> 
> My problem is that Lib/Server/Db/Adapters/FireBirdConnection.py does not 
> see Lib/Server/Db/DatabaseConnection.py.

Two solutions come into my mind:

1. Do not inherit from DatabaseConnection, but pass a DatabaseConnection object as a parameter to the FireBirdConnection's __init__function. After this you can delegate the necessary functionality to the DatabaseConnection (by using __getattr__). This introduces less coupling between DatabaseConnection and FireBirdConnection, event module testing is easier.

2. Maybe the layering of your application is wrong. If DatabaseConnection provides common functionality to the different Adapters, it should be on the same layer or one beneath the Adapters.

Hard to say which can be applied to your application without knowing more about it, though.

Kristóf




More information about the Python-list mailing list