Do big dock strings while wasting memory?
I understand that in Python, a string is just an expression, and the string itself will be garbage collected immediately after the control returns to the calling code, but ...
- Large class / method strings in your code: do they waste memory by making string objects up?
- Module-level document lines: are they stored indefinitely by the translator?
Does it even matter? My only concern came from the fact that if I use a large framework like Django or several large open source libraries, they tend to be very well documented, with potentially several megabytes of text. In these cases, are document lines loaded into memory for code that is used along the way, and then stored there or collected immediately like regular lines?
a source to share
-
"My understanding is that in Python a string is just an expression, and the string itself will be garbage collected immediately after the control returns to the calling code." I believe this is a misunderstanding. The dock is evaluated once (not on every function call) and stays alive at least as long as the function executes.
-
"Does it even matter?" when it comes to optimization, no answer, thinking about it abstractly but measuring. "Several megabytes" of text is probably not that much in a memory intensive application. The memory saver solution probably lives elsewhere and you can determine if the measurement matters.
-
Python
-OO
command line switch removes docstrings.
a source to share
Python dockstones are stored indefinitely by default as they are accessible via the __doc__ attribute of a function or module. For example, with the following in test.py:
"""This is a test module."""
def f():
"""This is a test function."""
pass
Then:
$ python
Python 2.5.1 (r251:54863, Oct 30 2007, 13:54:11)
[GCC 4.1.2 20070925 (Red Hat 4.1.2-33)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
>>> test.__doc__
'This is a test module.'
>>> test.f.__doc__
'This is a test function.'
>>>
The option -OO
to the interpreter seems to cause it to remove docstrings from the generated files .pyo
, but that doesn't have the effect I would expect:
$ python -OO
Python 2.5.1 (r251:54863, Oct 30 2007, 13:54:11)
[GCC 4.1.2 20070925 (Red Hat 4.1.2-33)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
>>> test.__file__
'/tmp/test.py'
>>>
$ grep "This is a test" /tmp/test.pyo
Binary file /tmp/test.pyo matches
$ python -OO
Python 2.5.1 (r251:54863, Oct 30 2007, 13:54:11)
[GCC 4.1.2 20070925 (Red Hat 4.1.2-33)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
>>> test.__file__
'/tmp/test.pyo'
>>> test.__doc__
'This is a test module.'
>>>
And in fact, the file test.pyo
generated with -OO
is identical to the file test.pyc
generated without command line arguments. Can anyone explain this behavior?
a source to share