Python interpreter (2): Code object

Source: Internet
Author: User

This is the second part of the Python interpreter introduction. Click here for the first part.

The three steps before compilation are to check a simple function object. Now let's talk about the deeper object-the Code object of the function.

 
 
  1. >>> def foo(a): 
  2. ...     x = 3 
  3. ...     return x + a 
  4. ... 
  5. >>> foo 
  6. <function foo at 0x107ef7aa0> 
  7. >>> foo.func_code 
  8. <code object foo at 0x107eeccb0, file "<stdin>", line 1> 

From the code above, you can find that the so-called code object is an attribute of the function object. This function object has many other attributes, but most of them are boring, because the foo function is too simple .)

The Code object is generated in the Python compiler and is interpreted in the interpreter. It transmits the "started" information to the interpreter. Let's take a look at the attributes of the Code object.

 
 
  1. >>> dir(foo.func_code) 
  2. ['__class__', '__cmp__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', 
  3. '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', 
  4. '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', 
  5. '__subclasshook__', 'co_argcount', 'co_cellvars', 'co_code', 'co_consts', 'co_filename', 
  6. 'co_firstlineno', 'co_flags', 'co_freevars', 'co_lnotab', 'co_name', 'co_names', 'co_nlocals', 
  7. 'co_stacksize', 'co_varnames'] 

Although this attribute list is long, most of the attributes are not involved today. We only focus on the attributes of three interesting foo Function Code objects.

 
 
  1. >>> foo.func_code.co_varnames 
  2. ('a', 'x') 
  3. >>> foo.func_code.co_consts 
  4. (None, 3) 
  5. >>> foo.func_code.co_argcount 

By calling them, we can get the variable name, the number of known constants in the function, and the number of function parameters in sequence. But so far, we still don't know what the command for generating code objects is. In fact, this command is calledBytecode. Bytecode is also an attribute of the Code object:

 
 
  1. >>> foo.func_code.co_code 
  2. 'd\x01\x00}\x01\x00|\x01\x00|\x00\x00\x17S' 

We get a lot of information to be decomposed. What happened here? In the next section, we will study bytecode in depth.

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.