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.

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Awesome, Thanks @bruno desthuilliers
@ApurvaKunkulol glad I could help - and feel free to accept the answer then ;)
Sure. will do that.
-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

2 Comments

Wrong. A module's top-level is only executed once (on the first import) for a given process.
Apologies, I did not take multiple imports into account.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.