gpt4 book ai didi

python - Python 中的标志是什么?

转载 作者:行者123 更新时间:2023-12-04 01:16:57 26 4
gpt4 key购买 nike

我在玩交互式 shell,测试新东西等。我发现一个函数有三个标志。

def bar(x,y):
pass

>>> dis.show_code(bar)
Name: bar
Filename: test.py
Argument count: 2
Positional-only arguments: 0
Kw-only arguments: 0
Number of locals: 2
Stack size: 1
Flags: OPTIMIZED, NEWLOCALS, NOFREE
Constants:
0: None
Variable names:
0: x
1: y
一个字符串也有 1 个标志
string = "s"

>>> dis.show_code(string)
Filename: <disassembly>
Flags: NOFREE
问题是我们为什么拥有它们或者我们为什么需要它们?

最佳答案

documentation说:

The following flag bits are defined for co_flags: bit 0x04 is set ifthe function uses the *arguments syntax to accept an arbitrary numberof positional arguments; bit 0x08 is set if the function uses the**keywords syntax to accept arbitrary keyword arguments; bit 0x20 is set if the function is a generator.

Future feature declarations (from __future__ import division) also usebits in co_flags to indicate whether a code object was compiled with aparticular feature enabled: bit 0x2000 is set if the function wascompiled with future division enabled; bits 0x10 and 0x1000 were usedin earlier versions of Python.

Other bits in co_flags are reserved for internal use.


在 Python 源代码中,您可以在 code.h 中找到更广泛的标志列表。 :
#define CO_OPTIMIZED    0x0001
#define CO_NEWLOCALS 0x0002
#define CO_VARARGS 0x0004
#define CO_VARKEYWORDS 0x0008
#define CO_NESTED 0x0010
#define CO_GENERATOR 0x0020
#define CO_NOFREE 0x0040
#define CO_COROUTINE 0x0080
#define CO_ITERABLE_COROUTINE 0x0100
#define CO_ASYNC_GENERATOR 0x0200
#define CO_FUTURE_DIVISION 0x20000
#define CO_FUTURE_ABSOLUTE_IMPORT 0x40000
#define CO_FUTURE_WITH_STATEMENT 0x80000
#define CO_FUTURE_PRINT_FUNCTION 0x100000
#define CO_FUTURE_UNICODE_LITERALS 0x200000
#define CO_FUTURE_BARRY_AS_BDFL 0x400000
#define CO_FUTURE_GENERATOR_STOP 0x800000
#define CO_FUTURE_ANNOTATIONS 0x1000000

关于python - Python 中的标志是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63191223/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com