New FAMILUG

The PyMiers

Wednesday 5 November 2014

[Python] python -v

Số man page mình đã từng đọc có thể... không thể đếm được (vì không nhớ)
nhưng buồn cười thay manpage của python thì lại chưa bao giờ đọc :v

Hôm nay đọc, và mò ra được một tí hay ho:

-v
Khi chạy python -v, python sẽ in ra một message mỗi lần một module được khởi tạo (initialized), hiển thị nơi mà nó được load (có thể là filename hoặc built-in). Đồng thời cung cấp các thông tin về quá trình dọn dẹp module khi kết thúc chương trình.
Rất hữu ích cho nhưng ai muốn tìm hiểu cơ chế hoạt động của python.
 $ echo -e print \'meomeo\' > /tmp/meo.py
 $ python -v /tmp/meo.py

# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
# /usr/lib/python2.7/site.pyc matches /usr/lib/python2.7/site.py
import site # precompiled from /usr/lib/python2.7/site.pyc
# /usr/lib/python2.7/os.pyc matches /usr/lib/python2.7/os.py
import os # precompiled from /usr/lib/python2.7/os.pyc
import errno # builtin
import posix # builtin
# /usr/lib/python2.7/posixpath.pyc matches /usr/lib/python2.7/posixpath.py

...
...
 Python 2.7.3 (default, Feb 27 2014, 20:00:17)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.

meomeo
# clear __builtin__._
# clear sys.path
# clear sys.argv
# clear sys.ps1

...
# cleanup sys
# cleanup __builtin__
# cleanup ints: 18 unfreed ints
# cleanup floats
Tham khảo
man 1 python
 -v     Print  a  message each time a module is initialized, showing the place (filename or built-in module) from which it is loaded.  When given twice, print a message for each file that is checked for when searching for a module.  Also provides information on module cleanup at exit.

No comments:

Post a Comment