Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

module

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()
like image 520
blackwind Avatar asked Oct 20 '22 06:10

blackwind


1 Answers

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.

like image 61
BrenBarn Avatar answered Oct 27 '22 18:10

BrenBarn