The callable object is the most important part in the python execution environment, such as python statements, assignments, expressions, and modules.
Objects are only a small part of the puzzle that Forms executable code blocks. These code blocks are called code objects. The core of each callable object is the code object. Generally, code objects can be executed as part of a function or method call, or using exec statements or built-in function eval. As a whole, the Code object of a python module,
Is all the code that constitutes the module. To execute python code, the code must first be converted to byte-compiled code, which is the real code object. However, they do not contain any information about their execution environment, which is why callable objects exist and are used to wrap a code object and provide additional information. Udf. func_code is the code object. The user-defined function object is the packaging of the Code object, while the method is the packaging of the function object. Python provides many built-in functions to support callable/executed objects. exec is used to execute code objects and complie is used to generate code objects.
- Callable (obj) determines whether obj is callable. If yes, True is returned; otherwise, False is returned.
- Compile (string, file, type) creates code objects from the type. file is the place where the code is stored, usually set''
There are three types of final types: 'eval' evaluable expressions (used together with eval () 'singles' single executable Statement (used together with exec) 'exec 'executable statement group (used with exec)
- Eval (obj, globals = globals (), locals = locals ())
Evaluate obj. obj is an expression compiled as a code object or a string expression. Global and local variables can use integers. Instead, an error is returned, and only strings and code objects are accepted.
The Execution Code object or string can be a file object (a valid opened python script) or a script file object >>> f = open ('try. py ') >>> exec f # display execution >>> exec f # No response is returned when the call is made again, because the file pointer has pointed to the end of the file >>> f. tell () # returns the position of the pointer to the current File> f. seek (0) # Return to the file header again> exec f # execution succeeded> f. close () # close the file
- Input (promp = '') is equivalent to eval (raw_input (promt = ''))
It can be seen that the input is eval, and raw_input is a simple string that uses exec to control code testing #! /Usr/bin/env python
DefFoo ():
Return True
DefBar ():
'Bar () does not do much'
Return
Foo. _ doc _ = 'foo () does not do much'
# Use a string to represent the test code
Foo. tester = """
If foo ():
Print 'passed'
Else:
Print 'failed'
"""
# Dir () Get the object list of the current namespace
ForEachAttrIn Dir():
Obj =Eval(EachAttr)
# Control to run function types only
If Isinstance(Obj,Type(Foo )):
If Hasattr(Obj, '_ doc __'):
Print'\ NFunction "% s" has a doc string: \ n \ t % s' % (eachAttr, obj. _ doc __)
If Hasattr(Obj, 'tester '):
Print'Function "% s" has a tester... executin '% eachAttr
ExecObj. tester
Else:
Print'Function "% s" has no tester... skipping '% eachAttr
Else:
Print'"% S" is not a function' % eachAttr
Publish by note