코드 개체에 대한 기본값 __init__
-
02-01-2020 - |
문제
Python에서 코드 개체를 찾았습니다.나는 생성자의 각 변수들 각각이 무엇을하는지 궁금합니다.내장 된 도움말 함수에서 그들에 대한 정보가 많이 있지 않습니다.
class code(object)
| code(argcount, nlocals, stacksize, flags, codestring, constants, names,
| varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])
|
| Create a code object. Not for the faint of heart.
.
그것은 분명히 매우 유익하지 않습니다.이러한 각 입력 각각은 어떤 유형이 기대하고 값은 무엇을합니까? 참고 : 나는이 질문을 학업 호기심에서 아닌 특정 코딩 목적을 위해 물었다.
해결책
파이썬 코드 객체는 대부분 속성에 대한 컨테이너입니다.생성자에 대해 표시되는 각 인수는 co_
접두어 (예 : argcount
인수가 co_argcount
속성이됩니다)가있는 속성이됩니다.
생성자는 약간의 유효성 검사를 수행하므로 인수가 올바른 유형이 아닌 경우 코드 객체가 나중에 켜기를 사용할 때만 실패하는 대신 예외를 발생시킵니다.
인수와 속성은 대부분 inspect
모듈의 문서 .관련 부분은 다음과 같습니다.
code co_argcount number of arguments (not including * or ** args)
co_code string of raw compiled bytecode
co_consts tuple of constants used in the bytecode
co_filename name of file in which this code object was created
co_firstlineno number of first line in Python source code
co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
co_lnotab encoded mapping of line numbers to bytecode indices
co_name name with which this code object was defined
co_names tuple of names of local variables
co_nlocals number of local variables
co_stacksize virtual machine stack space required
co_varnames tuple of names of arguments and local variables
.
EnteraCodiceTag 코드 및 co_freevars
속성은 내가 볼 수있는 한 문서화되지 않습니다.그들은 폐쇄와 관련이 있습니다.
제휴하지 않습니다 StackOverflow