blackwind
blackwind

Reputation: 153

Is there a Python analogue for JavaScript's module.exports?

In JavaScript, a module's default object can be set using "module.exports":

MyCache = require("./MyCache");
cache = new MyCache();

Similar behavior can be achieved in Python with:

from MyCache import Create as MyCache
cache = MyCache()

...but is it possible to set a default object in Python?

import MyCache
cache = MyCache()

Upvotes: 5

Views: 2499

Answers (1)

BrenBarn
BrenBarn

Reputation: 251355

No. When you import a module, you import a module. You can't make a module masquerade as something else. If you want to import a class, you can already do that very simply using from module import SomeClass as in your example.

Upvotes: 3

Related Questions