1

I'm wanting to trigger a function inside the module for when the module itself is imported;

From what I've tested it seems that I can just check if __name__ isn't __main__ and use that as a solution; but I was wondering if there was a better way with some sort of import hook?

if __name__ != '__main__':
    # I was imported
    ...

I'm wanting to modify an object that exists in the module for this specific case and only have it modified once.

I've had success with this; but am wondering if there's another way to approach this.

if __name__ == '__main__':
    example = 0
else:
    example = 1
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
jacob
  • 4,656
  • 1
  • 23
  • 32
  • 1
    Do you want this to happen for every time it's imported, or just the first time per interpreter? – user2357112 Jun 08 '16 at 21:54
  • Possible duplicate of [this](http://stackoverflow.com/questions/5027352/how-to-test-if-one-python-module-has-been-imported) – limbo Jun 08 '16 at 21:57
  • 1
    @limbo that question isn't from the right perspective; I want it from the module's perspective. – jacob Jun 08 '16 at 21:59
  • @user2357112 I'm wanting to modify an object that exists in the module for the case that's it's imported; and only modify it once. – jacob Jun 08 '16 at 22:00
  • Top level logic runs once on import. Various stdlib packages have convenience helpers that are generated on import. I believe re, rand, and os do this. – David Jun 08 '16 at 22:01
  • Additionally across the spectrum (stdlib and public) the try...except pattern is used to conditionally import libraries (eg cjson, easy json, and then falling back to the native implementation). All run once on first import. – David Jun 08 '16 at 22:03

1 Answers1

1

Python run's top level logic once on import. Examples can be found in the stdlib hashlib implementation here (github is down at moment). https://fossies.org/dox/Python-3.5.1/hashlib_8py_source.html#name=l00131

Notice the use of try...except for conditional imports and the top level lines starting at line #57

57 __always_supported = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512')
58 
59 algorithms_guaranteed = set(__always_supported)
60 algorithms_available = set(__always_supported)

Those are created once on import and not subsequent calls. Part of the reason why it works this way is that a module is an object and is stored in sys.modules - Py2 but same for Py3.

Edit: To clarify you can use modules like singletons but it is the path to madness as it can make unit-testing and debugging painful if not impossible.

David
  • 17,673
  • 10
  • 68
  • 97
  • Thank you for your answer; I don't believe I was specific enough in my question. I've since amended it and hopefully am being more thorough with what I'm looking for. – jacob Jun 08 '16 at 22:16
  • @jacob That's exactly how Python does it, especially for testing and in situations like unit-tests or applications that implement \_\_main\_\_ or am I missing something else in your question? – David Jun 08 '16 at 22:18
  • @jacob if it seems stupid but it works and someone can understand what it does, it might still be stupid but it works. – David Jun 08 '16 at 22:22