Python中的标志是什么?

2024-09-28 01:28:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我在玩交互式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

问题是我们为什么拥有它们,或者我们为什么需要它们


Tags: 函数onlystring标志defshowbarcode
2条回答

报告说:

The following flag bits are defined for co_flags: bit 0x04 is set if the function uses the *arguments syntax to accept an arbitrary number of 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 use bits in co_flags to indicate whether a code object was compiled with a particular feature enabled: bit 0x2000 is set if the function was compiled with future division enabled; bits 0x10 and 0x1000 were used in earlier versions of Python.

Other bits in co_flags are reserved for internal use.

在Python源代码中,您可以在^{}中找到更广泛的标志列表:

#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

这些FLAGS表示code对象的不同组件。当另一个答案指向正确的答案时,我只是给这个组合增加了一点乐趣

您可以尝试从dis导入FLAGS的子集

>>> from dis import COMPILER_FLAG_NAMES
>>> COMPILER_FLAG_NAMES
{1: 'OPTIMIZED',
 2: 'NEWLOCALS',
 4: 'VARARGS',
 8: 'VARKEYWORDS',
 16: 'NESTED',
 32: 'GENERATOR',
 64: 'NOFREE',
 128: 'COROUTINE',
 256: 'ITERABLE_COROUTINE',
 512: 'ASYNC_GENERATOR'}

然后您可以编写一个简单的函数,并查看FLAGS显示的内容:

>>> def foo:
        return

>>> dis.show_code(foo)
Name:              foo
Filename:          <ipython-input-138-32384d979b8a>
Argument count:    0
Kw-only arguments: 0
Number of locals:  0
Stack size:        1
Flags:             OPTIMIZED, NEWLOCALS, NOFREE
Constants:
   0: None

现在,您可以开始增加复杂性并了解其变化:

>>> def foo(a, *args, **kwargs):
        yield a
>>> dis.show_code(foo)
Name:              foo
Filename:          <ipython-input-141-56a68ddd064c>
Argument count:    1
Kw-only arguments: 0
Number of locals:  3
Stack size:        1
Flags:             OPTIMIZED, NEWLOCALS, VARARGS, VARKEYWORDS, GENERATOR, NOFREE
Constants:
   0: None
Variable names:
   0: a
   1: args
   2: kwargs

这里添加*args**kwargsyield分别添加了VARARGS, VARKEYWORDS, GENERATOR标志。 现在,我想了解更多信息:

>>> from asyncio import coroutine
>>> @coroutine
    async def foo(a, *args, **kwargs):
        yield b
        def bar():
            return foo(b)
        yield bar
>>> dis.show_code(foo)
Name:              coro
Filename:          C:\Users\sayan\Anaconda3\envs\tensorflow_gpu\lib\asyncio\coroutines.py
Argument count:    0
Kw-only arguments: 0
Number of locals:  4
Stack size:        8
Flags:             OPTIMIZED, NEWLOCALS, VARARGS, VARKEYWORDS, NESTED, GENERATOR, ITERABLE_COROUTINE
Constants:
   0: None
Names:
   0: base_futures
   1: isfuture
   2: inspect
   3: isgenerator
   4: isinstance
   5: CoroWrapper
   6: __await__
   7: AttributeError
   8: collections
   9: abc
  10: Awaitable
Variable names:
   0: args
   1: kw
   2: res
   3: await_meth
Free variables:
   0: func

在这里,添加@coroutinedecorator会给出NESTEDasync_def + yield会给出ITERABLE_COROUTINE,而freevar的存在,即函数bar,会移除NOFREE标志

相关问题 更多 >

    热门问题