1

If I have a module say A.py, wherein

import xxx
import yyy
class P:
 def __init__(self):
 pass
p = P()

Will the last line of code be executed every time I import A, in every module, or just once? I am attempting to make it a singleton instance, i.e. to be able to use only one instance throughout the session of execution. So please share, how that can be done. Thanks.

asked May 10, 2017 at 12:06
2
  • 4
    every time you import A (for the first time) in every module. Just add a print to convince yourself :) Commented May 10, 2017 at 12:07
  • for the first time in a same proces. Commented May 10, 2017 at 12:26

2 Answers 2

5

The code at a module's top level is only executed once per process (the first time the module is imported for this process). The module object instance is then stored in sys.modules and subsequent imports will access it from there.

The only caveat is when the same module is imported via 2 different qualified names, in which case it's treated as two distinct modules (hence the top-level is executed twice for the same process), but that's really a quite uncommon corner case.

Here's a simple example where you can see that in actions - mod.py is imported four time but executed only once (on the first import):

bruno@bigb:~/Work/playground/imptrace$ ls *.py
a.py b.py c.py main.py mod.py
bruno@bigb:~/Work/playground/imptrace$ cat mod.py
class P(object):
 def __init__(self):
 print "p %s initialzed" % id(self)
p = P()
bruno@bigb:~/Work/playground/imptrace$ cat a.py
print "in a.py"
import mod
bruno@bigb:~/Work/playground/imptrace$ cat b.py
print "in b.py"
import mod
bruno@bigb:~/Work/playground/imptrace$ cat c.py
print "in c.py"
import mod 
bruno@bigb:~/Work/playground/imptrace$ cat main.py
import a
import b
import c
import mod
bruno@bigb:~/Work/playground/imptrace$ python main.py
in a.py
p 139818712152976 initialzed
in b.py
in c.py

Edit: You state that

I am attempting to make it a singleton instance,

This won't make your class a singleton - nothing prevents someone to create other P instances. If you really want a singleton, you'll have to either override P.__new__() to prevent more than on single instance to be created (canonical singleton) or change P so that it only uses class attributes and classmethods, so that all instances will actually share the same attributes.

answered May 10, 2017 at 12:23
3
  • Awesome, Thanks @bruno desthuilliers Commented May 10, 2017 at 12:49
  • @ApurvaKunkulol glad I could help - and feel free to accept the answer then ;) Commented May 10, 2017 at 13:39
  • Sure. will do that. Commented May 11, 2017 at 6:58
-1

Yes - you are indeed correct. Every time you import this class, an object of type P will be created. Everything outside the class/function definition is effectively seen as an executable command

answered May 10, 2017 at 12:22
2
  • Wrong. A module's top-level is only executed once (on the first import) for a given process. Commented May 10, 2017 at 12:24
  • Apologies, I did not take multiple imports into account. Commented May 10, 2017 at 12:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.