memory overhead using from-import?

William Trenker wtrenker at shaw.ca
Sun Dec 21 06:31:49 EST 2003


William Trenker wrote:

> Could someone please confirm that if I code the following line in my own module:
> from cgi import escape
> that the only additional memory overhead from the cgi module will be that required to store the escape function's code.

After some experimenting with the /proc filesystem and a closer reading of the Python docs, I think I've answered my own question.  It looks like from-import does import the entire module.  So,
from cgi import escape
brings in the entire cgi module but only exposes the escape function to the importing module's namespace.

The code, below, set me straight.

Bill

Python 2.3.2 (#1, Oct 27 2003, 10:19:56) 
[GCC 2.95.3 20010315 (release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cgi
>>> import os,re
>>> pid = os.getpid()
>>> s = file('/proc/%s/status'%pid,'rb').read()
>>> print re.search('(VmSize:.*?kB).*(VmExe:.*?kB)',s,re.DOTALL).groups()
('VmSize:\t    5300 kB', 'VmExe:\t     712 kB')

Python 2.3.2 (#1, Oct 27 2003, 10:19:56) 
[GCC 2.95.3 20010315 (release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from cgi import escape
>>> import os, re
>>> pid = os.getpid()
>>> s = file('/proc/%s/status'%pid,'rb').read()
>>> print re.search('(VmSize:.*?kB).*(VmExe:.*?kB)',s,re.DOTALL).groups()
('VmSize:\t    5300 kB', 'VmExe:\t     712 kB')





More information about the Python-list mailing list