Internal parsing of the generator (generator)

Source: Internet
Author: User

#http://kb.cnblogs.com/page/87128/(not finished) 2.7. Generator (Generator)

The generator is an object returned by calling a generator function (generator functions), and is used more iteratively for collection objects.

    • __ITER__: is just an iterative marker.
    • Gi_code: The code object that corresponds to the generator.
    • Gi_frame: The frame object that the generator corresponds to.
    • Gi_running: Whether the generator function is executing. The generator function is in the frozen state after yield, before the next line of code that executes yield, at which point the value of this property is 0.
    • Next|close|send|throw: This is a few callable methods that do not contain metadata information, and how to use related documents that can view the generator.
DefGen ():
ForNInchXrange5):
YieldN
G=Gen ()
PrintG#<generator Object Gen at 0x...>
PrintG.gi_code#<code Object Gen at 0x...>
PrintG.gi_frame#<frame Object at 0x...>
print g.gi_ Running # 0
print G.next () # 0< Span style= "color: #008000;" >
print G.next () #< Span style= "color: #008000;" > 1
for n in g:
print N, Span style= "color: #008000;" ># 2 3 4

The next discussion is a few of the built-in object types that are not commonly used. These types should rarely be contacted during normal coding, unless you are implementing an interpreter or development environment yourself. So here are only a few properties, if you need a complete attribute table or want to learn more, you can view the reference documents listed at the end of the article.

2.8. Code block

Code blocks can be compiled from class source code, function source code, or a simple statement code. Here we only consider the case when it refers to a function, and we have mentioned in section 2.5 that you can use the Func_code property of the function to get to it. The properties of code are all read-only.

    • Co_argcount: The total number of normal parameters, excluding * parameters and * * parameters.
    • Co_names: A tuple of all parameter names (including * parameters and * * parameters) and local variable names.
    • Co_varnames: A tuple of all local variable names.
    • Co_filename: The file name where the source code resides.
    • Co_flags: This is a numeric value, and each bits contains specific information. The more concerned is 0b100 (0x4) and 0b1000 (0x8), if co_flags & 0b100! = 0, the description uses the *args parameter; if Co_flags & 0b1000! = 0, the **kwargs parameter is used. Also, if Co_flags & 0b100000 (0x20)! = 0, then this is a generator function (generator functions).
Co=Cat.sayHi.func_code
Print co.co_argcount # 1
Print co.co_names # (' name ',)
Print co.co_varnames # (' self ',)
print co.co_flags & 0b100 # 0 2.9. Stack frame (frame)

A stack frame represents a frame in a function call stack when the program runs. The function has no properties to fetch it because it is generated when the function is called, and the generator is returned by a function call, so there is a property pointing to the stack frame. To obtain a stack frame related to a function, it must be obtained when the function is called and the function has not been returned. You can get the current stack frame using the Sys module's _getframe () function, or the currentframe () function of the inspect module. The properties listed here are all read-only.

    • F_back: The previous frame of the call stack.
    • F_code: The code object corresponding to the stack frame.
    • F_locals: The same as the built-in function locals () when used on the current stack frame, but you can get the other frames first and then use this property to get the locals () of that frame.
    • F_globals: The same as the built-in function globals () when used on the current stack frame, but you can get other frames first ....
DefAdd (x, y=1 F = Inspect.currentframe ()
print F.f_locals # Same as locals ()
print F.f_back # <frame object at 0x...> return X+y
Add ( Span style= "color: #000000;" >2)

Generator (generator) internal parsing

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.