A discussion of the Python interpreter: The second part of the Code object

Source: Internet
Author: User

This article is part of the Python interpreter family of articles, and the first part of this article looks at this:

http://blog.csdn.net/caimouse/article/details/47342357

From the first part of the article, the main analysis of the function object related content. In this paper, we continue to analyze the content of python parser, mainly analyze the code object of function.

>>> def foo (a):

... x = 3

... return x + A

...

>>> Foo

<function Foo at 0x107ef7aa0>

>>> Foo.func_code

<code Object foo at 0x107eeccb0, file ' <stdin> ', line 1>

As you can see from the code above, the code object is a property of the function object (of course there are many other properties in the function object, which are not concerned with those properties for the time being).

The Python parser generates code objects after compiling and parsing, and the code object contains the information that the parser needs to execute the code, so let's look at the properties that the code object includes:

>>> dir (foo.func_code)

[' __class__ ', ' __cmp__ ', ' __delattr__ ', ' __doc__ ', ' __eq__ ', ' __format__ ', ' __ge__ ',

' __getattribute__ ', ' __gt__ ', ' __hash__ ', ' __init__ ', ' __le__ ', ' __lt__ ', ' __ne__ ', ' __new__ ',

' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ',

' __subclasshook__ ', ' co_argcount ', ' co_cellvars ', ' co_code ', ' co_consts ', ' co_filename ',

' Co_firstlineno ', ' co_flags ', ' co_freevars ', ' co_lnotab ', ' co_name ', ' co_names ', ' co_nlocals ',

' Co_stacksize ', ' co_varnames ']

As you can see from the above, there are a lot of properties here, and maybe you think it's stressful, but don't worry, today we just need to discuss a few related attributes. OK, so it's easy. Let's take a look at the code object for the function foo , where we only care about the following properties:

>>> Foo.func_code.co_varnames

(' A ', ' X ')

>>> foo.func_code.co_consts

(None, 3)

>>> Foo.func_code.co_argcount

1

Here you can see the relevant content: The name of the function foo variable, the constants used in the function, and the number of arguments to the function. But from here we don't see any properties related to executable directives, or how to execute code objects. We know that the parser generates executable instruction content called bytecode, which is stored in the properties of the code object:

>>> Foo.func_code.co_code

' D\x01\x00}\x01\x00|\x01\x00|\x00\x00\x17s '

The properties from these surfaces can be understood a lot, and in order to gain a deeper understanding of the code objects, we will continue the discussion in part three.


Source text See:

http://akaptur.com/blog/2013/11/17/introduction-to-the-python-interpreter-3/

Cai Junsheng No.:Shenzhencai shenzhen

A discussion of the Python interpreter: The second part of the Code object

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.