Jim
Jim

Reputation: 125

Deny access to imported module in a module

I have a module in my code called a.py which looks a bit like this:

import sqlite3
from sqlite3 import *
"""The point of importing twice in a different way is to
have a.py imported by someone and have all the sqlite3 functions (with some overriden by a.py)
but a.py still needs access to the source sqlite3 functions that he overrides"""
def connect(database): #overriding sqlite3's connect()
    print "It worked!"
    return sqlite3.connect(database)

and a file called b.py:

import a
a.sqlite3.connect("data.db")

I want to make the code in b.py invalid as no one should be able access the original (non-overriden) functions through a.py, how can I do this?

Upvotes: 0

Views: 66

Answers (2)

honza_p
honza_p

Reputation: 2093

What about this? Depending on the goal, it's sometimes possible to hide names inside functions / lambdas.

def connect(database): #overriding sqlite3's connect()
   import sqlite3
   print "It worked!"
   return sqlite3.connect(database)

But anyway, I don't think trying to hide modules in Python is reasonable.

Upvotes: 0

Moamen
Moamen

Reputation: 706

I'm afraid that you can't! Check here for more details: https://stackoverflow.com/a/1547163/1548838

But you still can use import sqlite3 as _sqlite3 and use it later like _sqlite3.connect. This will tell your module users (convention only) not to use this attribute.

And you still can use __all__ module variable as Ilja Everilä mentioned to prevent from a import *.

Upvotes: 1

Related Questions