Python Module Initialization Order?

I am a Python newbie coming from a C++ background. While I know it's not Pythonic to try to find a matching concept using my old C++ knowledge, I think this question is still a general question to ask:

Under C++, there is a well known problem called global/static variable initialization order fiasco, due to C++'s inability to decide which global/static variable would be initialized first across compilation units, thus a global/static variable depending on another one in different compilation units might be initialized earlier than its dependency counterparts, and when dependant started to use the services provided by the dependency object, we would have undefined behavior. Here I don't want to go too deep on how C++ solves this problem. :)

On the Python world, I do see uses of global variables, even across different .py files, and one typycal usage case I saw was: initialize one global object in one .py file, and on other .py files, the code just fearlessly start using the global object, assuming that it must have been initialized somewhere else, which under C++ is definitely unaccept by myself, due to the problem I specified above.

I am not sure if the above use case is common practice in Python (Pythonic), and how does Python solve this kind of global variable initialization order problem in general?

Thank you very much!


-------------Problems Reply------------

Under C++, there is a well known problem called global/static variable initialization order fiasco, due to C++'s inability to decide which global/static variable would be initialized first across compilation units,

I think that statement highlights a key difference between Python and C++: in Python, there is no such thing as different compilation units. What I mean by that is, in C++ (as you know), two different source files might be compiled completely independently from each other, and thus if you compare a line in file A and a line in file B, there is nothing to tell you which will get placed first in the program. It's kind of like the situation with multiple threads: you cannot say whether a particular statement in thread 1 will be executed before or after a particular statement in thread 2. You could say C++ programs are compiled in parallel.

In contrast, in Python, execution begins at the top of one file and proceeds in a well-defined order through each statement in the file, branching out to other files at the points where they are imported. In fact, you could almost think of the import directive as an #include, and in that way you could identify the order of execution of all the lines of code in all the source files in the program. (Well, it's a little more complicated than that, since a module only really gets executed the first time it's imported, and for other reasons.) If C++ programs are compiled in parallel, Python programs are interpreted serially.

Your question also touches on the deeper meaning of modules in Python. A Python module - which is everything that is in a single .py file - is an actual object. Everything declared at "global" scope in a single source file is actually an attribute of that module object. There is no true global scope in Python. (Python programmers often say "global" and in fact there is a global keyword in the language, but it always really refers to the top level of the current module.) I could see that being a bit of a strange concept to get used to coming from a C++ background. It took some getting used to for me, coming from Java, and in this respect Java is a lot more similar to Python than C++ is. (There is also no global scope in Java)

I will mention that in Python it is perfectly normal to use a variable without having any idea whether it has been initialized/defined or not. Well, maybe not normal, but at least acceptable under appropriate circumstances. In Python, trying to use an undefined variable raises a NameError; you don't get arbitrary behavior as you might in C or C++, so you can easily handle the situation. You may see this pattern:

except NameError:

which does nothing if duck does not exist. Actually, what you'll more commonly see is

except AttributeError:

which does nothing if duck does not have a method named quack. (AttributeError is the kind of error you get when you try to access an attribute of an object, but the object does not have any attribute by that name.) This is what passes for a type check in Python: we figure that if all we need the duck to do is quack, we can just ask it to quack, and if it does, we don't care whether it's really a duck or not. (It's called duck typing ;-)

Python import executes new Python modules from beginning to end. Subsequent imports only result in a copy of the existing reference in sys.modules, even if still in the middle of importing the module due to a circular import. Module attributes ("global variables" are actually at the module scope) that have been initialized before the circular import will exist.

import a

var1 = 'foo'
import b
var2 = 'bar'

import a
print a.var1 # works
print a.var2 # fails

Category:python Views:0 Time:2010-06-21

Related post

  • Python Module Initialization 2011-09-13

    Is it bad practice to initialize the objects in the module, in the module code? in class _Foo(object): def __init__(self): self.x = 'Foo' Foo = _Foo() Than in user code, you could: >>> from Module import Foo >>> print Foo

  • How to control the order of module initialization in Prism 2009-08-18

    I'm using Prism V2 with a DirectoryModuleCatalog and I need the modules to be initialized in a certain order. The desired order is specified with an attribute on each IModule implementation. This is so that as each module is initialized, they add the

  • How to avoid latencies with Python module globals initialization? 2012-02-21

    I'm trying to optimize the general load time of a web application written in python. My application uses a lot of modules, some of which might or might not be actually needed for a given request. Since page load time is an important factor of the end

  • Python module for converting PDF to text 2008-08-25

    Is there any python module to convert PDF files into text? I tried one piece of code found in Activestate which uses pypdf but the text generated had no space between and was of no use. --------------Solutions------------- Try PDFMiner. It can extrac

  • problems importing python module 2011-06-03

    I'm trying to use python's bitstring module in a script and am getting an import error. This error does not happen when running from interactive mode. Here's the code: import bitstring b = bitstring.BitArray(bin='001001111') When run like this: pytho

  • Is there a fini routine for a python module written in C? 2009-04-17

    I have a python module written in C, and I would like to add a function that is called when the module is unloaded. I obviously have an initfoo function to initialize the module -- is there a way to tell python to call a finifoo function when it's un

  • Does Python have an ordered set? 2009-10-31

    Python has an ordered dictionary, what about an ordered set? --------------Solutions------------- There is an ordered set recipe for this which is referred to from the Python Documentation. This runs on Py2.6 or later and 3.0 or later without any mod

  • Install Plone egg as a Python module on Windows 2010-01-08

    I have a Plone site (Plone version 3.1.2) that I need to install a product called GrufSpaces on - ( However, it is a production site and so I can't easily take it down to upgrade Plone to 3.2+ in order to use bui

  • Why can't the linker prevent the C++ static initialization order fiasco? 2010-02-20

    EDIT: Changed example below to one that actually demonstrates the SIOF. I am trying to understand all of the subtleties of this problem, because it seems to me to be a major hole in the language. I have read that it cannot be prevented by the linker,

  • Is there a python module to parse Linux's sysfs? 2011-01-10

    Hey all, Linux has a lot of great features in procfs and sysfs, and tools like vmstat extend that quite a bit, but I have a need to collect data from a variety of these systems and was hoping to leverage a unified Python utility instead of hacking to

  • libtool python module linking and installation 2011-02-10

    I work on a project written in C, using the autotools, that produces a static library. Am also providing a Python interface to the C API in the context of the same project. I did not have permission until recently to introduce libtool in the project,

  • How to find out what methods, properties, etc a python module possesses 2011-02-24

    Sorry if my question is unclear, I'm new to python and still coming to grips with its terminology. My question maybe a bit general. Lets say I import a module, in order for me to make the best use of it, I would like to know what properties, methods,

  • How do I get IntelliJ to recognize common Python modules? 2011-05-23

    I'm using IntelliJ 10 IDEA Ultimate Edition. I've created a new file, and IntelliJ has correctly switched to Python parsing mode. (I can confirm this by typing "d", it pops up "def" as a suggestion, and hitting tab correctly gives me "def :")

  • Using pip to install single-file python modules 2011-05-27

    I'm wondering if there's a way to "install" single-file python modules using pip (i.e. just have pip download the specified version of the file and copy it to site-packages). I have a Django project that uses several 3rd-party modules which aren't pr

  • How do I run a function in a Python module using only the Windows command line? 2011-07-15

    To clarify, the Python module I'm writing is a self-written .py file (named converter), not one that comes standard with the Python libraries. Anyways, I want to somehow overload my function such that typing in converter file_name will send the file'

  • Trying to build a Python class in a C extension that can be instantiated from a Python module 2011-08-27

    I'm trying to build an exception class that I can use both in a C extension and the Python modules that import the extension. I'm using the PyErr_NewException to create the class as a superclass of the Exception class, but I can't figure out how to c

  • How to split a Python module into multiple files? 2011-10-17

    I have a single Python module which contains 3 classes: A, A1 and A2. A1 and A2 derive from A. A contains functions which operate on A1 and A2. This all works fine when it's in one .py file. But that file has grown quite long and I would like to spli

  • Best practice for hacking on a 3rd-party python module 2011-10-23

    I often find myself wanting to use a 3rd party python module in my own project, but I know that I will also need to make changes to the 3rd party module that I want to push upstream. What is the best practice of file layout/installation to achieve th

  • trouble compiling and installing cantera python module 2011-11-17

    I'm attempting to install the Cantera as a python module within Ubuntu linux. For reference I used these instructions: Installing Cantera. After running ./preconfig and make, I get the following error: fatal error: numarray/arrayobject.h: No such fil

Copyright (C), All Rights Reserved.

processed in 0.115 (s). 11 q(s)