Python BASICS (21): callable and executable object, 2015 python

Source: Internet
Author: User

Python BASICS (21): callable and executable object, 2015 python

There are multiple methods to run external programs in Python, such as running operating system commands or other Python scripts, executing files on a disk, or running files through the network. It depends entirely on what you want to do. Specific environments include:
Continue running in the current script
Create and Manage Sub-Processes
Execute External commands or programs
Run the command
Call commands over the network
Run the command to create the output to be processed
Execute other Python scripts
Execute a series of dynamically generated Python statements
Import Python Module
In Python, both internal and external modules can provide the above functions. Programmers must select appropriate processing methods from these modules based on their implementation needs.

Callable object

Many Python objects are called. They are called by any function operator. Python has four callable objects: functions, methods, classes, and some class instances.

1. Functions
Python has three different types of function objects. The first type is built-in functions.
Built-in functions (BIFs)
BIF is written in C/CPP. After compilation, it is written into the Python interpreter and loaded into the system as part of the first (built-in) namespace. These functions are in the _ builtin _ module and imported to the interpreter as the _ builtins _ module.
You can use dir () to list all the attributes of a function:

>>> dir(type)['__abstractmethods__', '__base__', '__bases__', '__basicsize__', '__call__', '__class__', '__delattr__', '__dict__', '__dictoffset__', '__doc__', '__eq__', '__flags__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__instancecheck__', '__itemsize__', '__le__', '__lt__', '__module__', '__mro__', '__name__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasscheck__', '__subclasses__', '__subclasshook__', '__weakrefoffset__', 'mro']


According to the internal mechanism, because BIFs and built-in method (BIMs) belong to the same type, the result of calling type () for BIF or BIM is:

>>> type(dir)<type 'builtin_function_or_method'>

 

User-Defined Function (UDF)
Udfs are usually written in Python and are defined at the highest level of the module. Therefore, udfs are mounted to the system as part of the global namespace. Functions can also be defined in other functions. We can access attributes in multiple nested scopes.
According to the internal mechanism, user-defined functions are of the "function" type:

>>> def foo():pass>>> type(foo)<type 'function'>

 

Lambda expressions
Lambda expressions are slightly different from user-defined functions. Although they also return a function object, they are not created using the def statement, but with the lambda Keyword:
Lambda expressions do not provide the basic structure for the code bound to the name. Therefore, they must be called through functional programming interfaces or their references must be assigned to a variable, then you can directly call the API or call the API through a function. A variable is only an alias and is not the name of a function object.
In addition to being named, the object for creating a function through lambda has the same attributes as the UDF; __name _ or func_name attribute to the positioning string "<lambda>"

>>> lambdaFunc = lambda x: x * 2>>> lambdaFunc(12)24>>> type(lambdaFunc)<type 'function'>>>> lambdaFunc.__name__'<lambda>'

 


The UDF name is as follows:

>>> def foo():pass>>> foo.__name__'foo'

 

The above are three function objects.

2. Method
A user-defined method is a function defined as part of a class. Many Python data types, such as lists and dictionaries, also have methods, which are called built-in methods. To describe the type of ownership, the method is named by the object name and the period property identifier.

Built-in method (BIMs)
We just talked about the similarities between BIF and BIM. Only built-in methods are available for built-in types. For the built-in method, the type () factory function provides the same output as BIF.

>>> type([].append)<type 'builtin_function_or_method'>

 

In addition, BIM and BIF have the same attributes. The difference is that the _ self _ attribute of BIM points to a Python object, and BIF points to None.

User-Defined method (UDM)
UDM is included in the class definition. It only has the packaging of standard functions and can be used only by the class that defines them. If they are not covered in the subclass definition, you can call them through the subclass instance.

3. Class
The result of the call class is that the instance is created, that is, the instance is instantiated.

4. instance of the class
Python provides a special method named _ call _ for the class. This method allows programmers to create callable objects (instances ). By default, the __call _ () method is not implemented, which means that the instance cannot be called in most cases. However, if this method is overwritten in the class, the instance of this class will become callable. Calling such an instance object is equivalent to calling the _ call _ () method. For example, foo () and foo. _ call _ (foo) has the same effect. foo is also used as a parameter here. Because it is a reference to itself, the instance will automatically become the first parameter for each method call, if _ call _ () has a parameter, foo (arg) is equivalent to foo. _ call _ (foo, arg) is the same.

 

Code object

Callable objects are the most important part of the Python execution environment, but not all of them. Python statements, assignments, expressions, and even modules constitute a larger scene. These executable objects cannot be called as callable objects. These code blocks are called code objects.

The core of each callable object is a code object consisting of statements, assignments, expressions, and other callable objects. Viewing a module means observing a large object that contains all the code in the module. Then the code is divided into statements, values, expressions, and callable objects. The callable object can be recursively decomposed to the next layer, where it has its own code object.
Generally, code objects can be executed as part of a function or method call, and can also be executed using exec statements or built-in function eval. As a whole, the Code object of a Python module is all the code of this module.
To execute Python code, the code must first be converted into byte-compiled code (also known as bytecode ). This 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 has the udf. func_code attribute, which is the code object. UDM. im_func of udm is also a function object, which also has its own udm. im_func.func_code object. In this way, you will find that the function object is only the packaging of the Code object, and the method is to wrap the function object. The bottom layer is a code object.

 


Executable object declarations and built-in functions

Python provides a large number of BIF to support callable/executable objects.

1. callable ()
Callable () is a Boolean function that determines whether an object can be called using the function operator. If you can call it, True is returned; otherwise, False is returned.

2. compile ()
The compile () function allows programmers to quickly generate code objects at runtime. Then they can use exec statements or the built-in function eval () to execute these objects or evaluate them.
The three parameters of compile are required. The first parameter represents the Python code to be compiled. The second parameter is a string. Although required, it is usually set as an empty string. This parameter represents the name of the file containing the Code object (string type ). Compile is usually used to dynamically generate Python code in the form of strings, and then generate a code object-the code is obviously not stored in any file. The final parameter is a string used to indicate the code type. There are three possible values:
'Eval' evaluable expression [used with eval ()]
'Singles' single executable statement [used with exec]
'Exec 'can be executed with the cast [used with exec]

Evaluable expression

>>> eval_code = compile('10-2','','eval')>>> eval(eval_code)8

 

Single executable statement

>>> single_code = compile('print "Hello world"','','single')>>> single_code<code object <module> at 024DC698, file "", line 1>>>> exec single_codeHello world

 

Executable statement Group

>>> exec_code = compile("""req = input('Count how many numbers?')for eachNum in range(req):  print eachNum""",'','exec')>>> exec exec_codeCount how many numbers?6012345

 

3. eval ()
Eval () evaluates the expression. The expression can be a pre-compiled code object created by a string or a built-in function compile. This object is the first and most important parameter. The second and third parameters are optional, representing the objects in the global and local namespaces, respectively. If the two parameters are given, the global data must be a dictionary, and the local data can be any ing object. If these two parameters are not provided, the default values are the objects returned by globals () and locals. If only one global dictionary is passed in, the dictionary is also passed in as a local parameter.
This is an example of eval.

>>> eval('123')123>>> int('123')123>>> eval('123+234')357>>> int('123+234')Traceback (most recent call last):File "<pyshell#10>", line 1, in <module>int('123+234')ValueError: invalid literal for int() with base 10: '123+234'

 

At the beginning, when we pass '100' to eval () and int (), the same result is returned, but the methods are different. eval () the string enclosed in quotation marks is used as a Python expression to evaluate the value. int () accepts a string representing an integer and converts it to an integer. But when we enter '100 + 123', the situation is different. Int () fails to be called. The eval () function turns a blind eye to the quotation marks at both ends of the expression and runs it on the interpreter to return the result.

4. exec
Similar to eval (), exec statements execute Python code in the form of code objects or strings. Similarly, using compile () to pre-compile duplicate code helps improve performance, because it does not have to be compiled in bytes during calls. Exec only accepts one parameter. Syntax:

exec obj

Obj can be an original string, such as a single statement or statement group, or a code object at the pre-compilation layer.

>>> exec """x = 0print 'x is currently:',xwhile x < 5:  x += 1  print 'incrementing x to:',x"""x is currently: 0incrementing x to: 1incrementing x to: 2incrementing x to: 3incrementing x to: 4incrementing x to: 5

 


Exec can also accept valid Python file objects. If we use the above multi-line code to create an xcount. py file, we can also execute the same code as below:

>>> f = open('xcount.py') # open the file>>> exec f # execute the filex is currently: 0incrementing x to: 1incrementing x to: 2incrementing x to: 3incrementing x to: 4incrementing x to: 5

 

We have called the file f above. If you continue to call it after it is complete

>>> exec f>>>


The call fails. It is not a real failure, but it does not do anything. In fact, exec has read all the data from the file and stays at the end of the file (EOF ). When exec is called with the same file object, there is no executable code, so exec does nothing.
We can use the tell () method to tell us where the file is located, and then use OS. path. getsize () to tell us the script size. The two numbers are exactly the same:

>>> f.tell()116>>> f.close()>>> from os.path import getsize>>> getsize('xcount.py')116

 

If you want to run it again without closing or re-opening the file, you can use seek () to start the file and call exec again. Suppose we haven't called f. close (), then:

>>> f.seek(0)>>> exec fx is currently: 0incrementing x to: 1incrementing x to: 2incrementing x to: 3incrementing x to: 4incrementing x to: 5>>> f.close()

 

5. input ()
The previously used built-in function input () is a combination of eval () and raw_input (). It is equivalent to eval (raw_input (), input (), and raw_input () there is also an optional parameter to prompt the user string.
Input is different from raw_input (). The data returned by input () is the result of evaluating the input expression and is a Python object.

6. Use Python to generate and execute Python code at runtime
Two examples are provided in the book. These two examples are executed at runtime using the Python code as a string.
The first example is the loopmake. py script. A simple, fast, and cyclic computer-aided software project. Prompt the user to give various parameters, generate a code string, and execute it.

dashes = '\n' + '-' * 50exec_dict = {'f':'''               #for loopfor %s in %s:    print %s''','s':'''               # sequence while loop%s = 0%s = %swhile %s < len(%s):    print %s[%s]    %s = %s + 1''','n':'''                # counting while loop%s = %dwhile %s < %d:    print %s    %s = %s + %d'''}def main():    ltype = raw_input('Loop type? (For/While)')    dtype = raw_input('Data type? (Number/Sequence)')    if dtype == 'n':        start = input('Starting value? ')        stop = input('Ending value (non-inclusive)? ')        step = input('Stepping value? ')        seq = str(range(start, stop, step))    else:        seq = raw_input('Enter sequence:')    var = raw_input('Iterative variable name?')    if ltype == 'f':        exec_str = exec_dict['f'] % (var, seq, var)    elif ltype == 'w':        if dtype == 's':            svar = raw_input('Enter sequence name? ')            exec_str = exec_dict['s'] % \                       (var, svar, seq, var, svar, svar, var, var, var)        elif dtype == 'n':            exec_str = exec_dict['n'] % \                       (var, start, var, stop, var, var, var, step)    print dashes    print 'The custom-generated code for you is:' + dashes    print exec_str + dashes    print 'The execution of the code:' + dashes    exec exec_str    print dashesif __name__ == '__main__':    main()    

 

If you are interested, you can execute this code, which is very interesting and can help you generate and execute the code. When I write this code, I feel the power of exec and input.

The second example is conditional code execution.
This is the code:

def foo():    return Truedef bar():    'bar() does not do much'    return Truefoo.__doc__ = 'foo() does not do much'foo.tester = '''if foo():    print 'PASSED'else:    print 'FAILED''''for eachAttr in dir():    obj = eval(eachAttr)    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... executing'\                  % eachAttr            exec obj.tester        else:            print 'Function "%s" has no tester... skipping'\                  % eachAttr    else:        print '"%s" is not a function' % eachAttr

 

The following is the execution result:

>>> "__builtins__" is not a function"__doc__" is not a function"__file__" is not a function"__name__" is not a function"__package__" is not a functionFunction "bar" has a doc string:bar() does not do muchFunction "bar" has no tester... skippingFunction "foo" has a doc string:foo() does not do muchFunction "foo" has a tester... executingPASSED

 

The code is not hard to understand, but what it does is really interesting, isn't it?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.