Hey,
Suppose I have a Python application consists of many modules (lets say
it is a Django application).
If all the modules files are importing sys module, how many times the
sys module will be compiled and executed?
Only once (the first time the PVM locates, compiles and executes the
sys module)? or once for each module importing sys?
Thanks. 7 1488
Hussein B a écrit :
Hey,
Suppose I have a Python application consists of many modules (lets say
it is a Django application).
If all the modules files are importing sys module, how many times the
sys module will be compiled and executed?
Only once (the first time the PVM locates, compiles and executes the
sys module)? or once for each module importing sys?
Hmmm.... Well, could it be possible that Python's developper were smart
enough to implement some cache mechanism here ?-)
import do (approximately) the following:
check if the module already exists in sys.modules
if not:
locate the module's source in sys.path
if not found
raise an import error
look for a compiled .py file
if there's none, or if there's one and it's older than the .py file:
compile the source and save it as .pyc
execute the .pyc file and build a module object from it
add the module object to sys.modules
bind the relevant names into your current namespace
On Aug 20, 8:08*pm, Hussein B <hubaghd...@gmail.comwrote:
Hey,
Suppose I have a Python application consists of many modules (lets say
it is a Django application).
If all the modules files are importing sys module, how many times the
sys module will be compiled and executed?
Only once (the first time the PVM locates, compiles and executes the
sys module)? or once for each module importing sys?
Thanks.
sys is a built-in module, so the answer is zero times.
For a non-builtin module foo where there exists:
(1) only a foo.py, it will be compiled into foo.pyc
(2) only a foo.pyc, it will be used
(3) both a foo.py and a foo.pyc, Python compiles the foo.py if the pyc
is out of date or (so I believe[*]) was created by a different
version of Python.
Subsequent imports will use the in-memory copy (in sys.modules, IIRC[*]) ...
[*] == Please save me the bother of checking this in the manual :-)
HTH,
John
On Aug 20, 5:43 am, John Machin <sjmac...@lexicon.netwrote:
On Aug 20, 8:08 pm, Hussein B <hubaghd...@gmail.comwrote:
Hey,
Suppose I have a Python application consists of many modules (lets say
it is a Django application).
If all the modules files are importing sys module, how many times the
sys module will be compiled and executed?
Only once (the first time the PVM locates, compiles and executes the
sys module)? or once for each module importing sys?
Thanks.
sys is a built-in module, so the answer is zero times.
For a non-builtin module foo where there exists:
(1) only a foo.py, it will be compiled into foo.pyc
(2) only a foo.pyc, it will be used
(3) both a foo.py and a foo.pyc, Python compiles the foo.py if the pyc
is out of date or (so I believe[*]) was created by a different
version of Python.
Subsequent imports will use the in-memory copy (in sys.modules, IIRC[*]) ... [*] == Please save me the bother of checking this in the manual :-)
HTH,
John
One more question:
If I have this structure:
orig/com/domain/project/Klass1.py
Klass2.py
__init__.py
Why com, domain, project should have __init__.py also? they don't
contain source code files?
Thanks again.
Hussein B a écrit :
(snip)
One more question:
If I have this structure:
orig/com/domain/project/Klass1.py
Klass2.py
__init__.py
Why com, domain, project should have __init__.py also?
Yes, why should they ? Unless you want to mimic Java's package system so
you do "import com.domain.project.stuff" - which is IMHO a pretty bad
idea -, there's just no reason to turn all your filesystem into a python
package.
Python's philosophy here is that flat is better than nested.
On Aug 20, 6:39 am, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
Hussein B a écrit :
(snip)
One more question:
If I have this structure:
orig/com/domain/project/Klass1.py
Klass2.py
__init__.py
Why com, domain, project should have __init__.py also?
Yes, why should they ? Unless you want to mimic Java's package system so
you do "import com.domain.project.stuff" - which is IMHO a pretty bad
idea -, there's just no reason to turn all your filesystem into a python
package.
Python's philosophy here is that flat is better than nested.
Sorry I don't follow you :(
Module package is also nested.
Hussein B wrote:
On Aug 20, 6:39 am, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
>Hussein B a écrit :
>>One more question: If I have this structure: orig/com/domain/project/Klass1.py Klass2.py __init__.py Why com, domain, project should have __init__.py also?
Yes, why should they ? Unless you want to mimic Java's package system so you do "import com.domain.project.stuff" - which is IMHO a pretty bad idea -, there's just no reason to turn all your filesystem into a python package.
Python's philosophy here is that flat is better than nested.
Sorry I don't follow you :(
Module package is also nested.
Essentially, "flatter is better than more nested" -- in other words
the things you see after "import this" are principles to trend towards,
not commandments to never be violated. Some structure is better
than no structure, but that doesn't mean more structure is better
ad infinitum. Lists of length 1, modules containing a single class,
dictionaries with a single entry, functions or methods that simply
return a variable or instance are "code smells" for over-exercise of
structuring for its own sake. Each layer should have enough structure
that it is "worth" replacing the more straightforward access with a
name.
Similarly, the name should be associated with few enough things
that you might reasonably expect to hold its scope in your head.
A layer with fifty elements "smells" as bad as one with a single
element, just in a different way.
--Scott David Daniels Sc***********@Acm.Org
Bruno Desthuilliers wrote:
As the name imply, built-in modules are built in the interpreter - IOW,
they are part of the interpreter *exposed* as modules[1]. Unless you
have a taste for gory implementation details, just don't worry about this.
Other "ordinary" modules need of course to be executed once when first
loaded - IOW, the first time they are imported. All statements[2] at the
top-level of the module are then sequentially executed, so that any
relevant object (functions, classes, whatever) are created and bound in
the module's namespace.
Builtin modules like thread, signal etc. as well as C extensions like
socket have an initialization function. The initialization function is
called during the first import of a module. It creates the module object
and assigns objects to its __dict__ attribute.
Builtin modules are statically linked into the Python executable /
library while normal C extensions are shared libraries. Some modules are
builtins because they are required during boot strapping. It's possible
to embed most C modules into the core.
Christian This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Al Murphy |
last post by:
Hi,
I hope that you can help me wit this one please and apologies if
i'm in the wrong forum! I recently bought a book that contaiend a
CD-ROM of...
|
by: Frantisek Fuka |
last post by:
This thing keeps bugging me. It's probably some basic misunderstanding
on my part but I am stumped. Let's say I have two Python files: file.py
and...
|
by: Player |
last post by:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hello
I am teaching myself python, and I have gotten a long way, it's quite a
decent language...
|
by: Awah Teh |
last post by:
I am working on a project that involves importing IIS Log files into a SQL
Server database (and these logfiles are big --> Some up to 2GB in size)....
|
by: Sara |
last post by:
Hi -
I've been reading the posts for a solution to my query, and realize
that I should ask an "approch" question as well.
We receive our...
|
by: ANSWER |
last post by:
Is there any way I could disable user to create a new database and import
all of my tables and queries into his database.
I make .mde, disable...
|
by: Timothy Shih |
last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I
wrote a simple function which takes in 2 buffers (one a byte buffer, one a...
|
by: Rufus A |
last post by:
I've been asked to extract some data from tables in Oracle for
importing in to Lotus Notes, and am struggling over the SQL which is
probably very...
|
by: TG |
last post by:
hi!
I am trying to import a text file into a table created on the fly in
SQL Server 2005 since the number of columns can vary depending on what...
|
by: concettolabs |
last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
|
by: better678 |
last post by:
Question:
Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct?
Answer:
Java is an object-oriented...
|
by: teenabhardwaj |
last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: CD Tom |
last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
| |