Python-built-in functions

From 탱이의 잡동사니
Jump to navigation Jump to search

Overview

Python built-in function 내용 정리

Basic

최신 버전의 문서는 아래의 링크에서 확인할 수 있다.

reload

지정된 모듈을 reload 한다. reload() 시, 해당 모듈을 참조하는 object 가 있다면, reference 에러가 발생하기 때문에 사용에 주의해야 한다.

Reload a previously imported module. The argument must be a module object, so it must have been successfully imported before. This is useful if you have edited the module source file using an external editor and want to try out the new version without leaving the Python interpreter. The return value is the module object (the same as the module argument).

When reload(module) is executed:

  • Python modules’ code is recompiled and the module-level code reexecuted, defining a new set of objects which are bound to names in the module’s dictionary. The init function of extension modules is not called a second time.
  • As with all other objects in Python the old objects are only reclaimed after their reference counts drop to zero.
  • The names in the module namespace are updated to point to any new or changed objects.
  • Other references to the old objects (such as names external to the module) are not rebound to refer to the new objects and must be updated in each namespace where they occur if that is desired.

There are a number of other caveats:

When a module is reloaded, its dictionary (containing the module’s global variables) is retained. Redefinitions of names will override the old definitions, so this is generally not a problem. If the new version of a module does not define a name that was defined by the old version, the old definition remains. This feature can be used to the module’s advantage if it maintains a global table or cache of objects — with a try statement it can test for the table’s presence and skip its initialization if desired:

<source lang=python> try:

   cache

except NameError:

   cache = {}

</source> It is generally not very useful to reload built-in or dynamically loaded modules. Reloading sys, __main__, builtins and other key modules is not recommended. In many cases extension modules are not designed to be initialized more than once, and may fail in arbitrary ways when reloaded.

If a module imports objects from another module using from ... import ..., calling reload() for the other module does not redefine the objects imported from it — one way around this is to re-execute the from statement, another is to use import and qualified names (module.*name*) instead.

If a module instantiates instances of a class, reloading the module that defines the class does not affect the method definitions of the instances — they continue to use the old class definition. The same is true for derived classes.

Example

Wrong reload () usage. It makes None type reference error.

# setting default handlers    
2017-05-26 17:03:23.732947 [ERR] switch_cpp.cpp:1237 Could not set default handlers. err[super(type, obj): obj must be an instance or subtype of type], detail[Traceback (most recent call last):
  File "/usr/lib64/python2.6/site-packages/test/voicemail/test_message.py", line 493, in handler
    lvm = leave_message(session, args)
  File "/usr/lib64/python2.6/site-packages/test/voicemail/test_message.py", line 43, in __init__
    super(leave_message, self).__init__(session, args)
  File "/usr/lib64/python2.6/site-packages/test/voicemail/test_base.py", line 61, in __init__
    super(voicemail_base, self).__init__(session, args)
TypeError: super(type, obj): obj must be an instance or subtype of type
]

compileall

지정된 모듈을 byte-compile 한다.

Example

$ python -m compileall ./*

See also