How to get function parameters from disHow to merge two dictionaries in a single expression?How do I check if a list is empty?How do I check whether a file exists without exceptions?How can I safely create a nested directory in Python?How to get the current time in PythonUsing global variables in a functionHow do I sort a dictionary by value?How to make a chain of function decorators?How do I get the number of elements in a list in Python?How do I list all files of a directory?
A workplace installs custom certificates on personal devices, can this be used to decrypt HTTPS traffic?
Can I use my Chinese passport to enter China after I acquired another citizenship?
The most efficient algorithm to find all possible integer pairs which sum to a given integer
Hostile work environment after whistle-blowing on coworker and our boss. What do I do?
Pronouncing Homer as in modern Greek
Meta programming: Declare a new struct on the fly
Can a Bard use an arcane focus?
Resetting two CD4017 counters simultaneously, only one resets
Visiting the UK as unmarried couple
Female=gender counterpart?
My boss asked me to take a one-day class, then signs it up as a day off
Should a half Jewish man be discouraged from marrying a Jewess?
I2C signal and power over long range (10meter cable)
Invariance of results when scaling explanatory variables in logistic regression, is there a proof?
Would it be legal for a US State to ban exports of a natural resource?
Do all polymers contain either carbon or silicon?
Are Warlocks Arcane or Divine?
What does the "3am" section means in manpages?
Freedom of speech and where it applies
Is there an Impartial Brexit Deal comparison site?
A known event to a history junkie
What was required to accept "troll"?
How to deal with or prevent idle in the test team?
Bob has never been a M before
How to get function parameters from dis
How to merge two dictionaries in a single expression?How do I check if a list is empty?How do I check whether a file exists without exceptions?How can I safely create a nested directory in Python?How to get the current time in PythonUsing global variables in a functionHow do I sort a dictionary by value?How to make a chain of function decorators?How do I get the number of elements in a list in Python?How do I list all files of a directory?
How to get list of function parameters in python from code object.
import xdis.std as dis
ops = list(dis.Bytecode("""def f(a, b):
return 1"""))
print(ops)
code_obj = ops[0]
print(list(dis.Bytecode(code_obj.argval)))
The above code uses xdis module to decompile code which needs to be installed via pip. However the code object is a normal one which you would expect from the default dis module in python.
I want to know how to get the list of function parameters in order. In this case, a and b
This is the output in interactive python -
>>> import xdis.std as dis
>>> ops = list(dis.Bytecode("""def f(a, b):
... return 1"""))
>>> print(ops)
[Instruction(opname='LOAD_CONST', opcode=100, optype='const', inst_size=2, arg=0, argval=<code object f at 0x7a1a5c4f60, file "<disassembly>", line 1>, argrepr='<code object f at 0x7a1a5c4f60, file "<disassembly>", line 1>', has_arg=True, offset=0, starts_line=1, is_jump_target=False), Instruction(opname='LOAD_CONST', opcode=100, optype='const', inst_size=2, arg=1, argval='f', argrepr="'f'", has_arg=True, offset=2, starts_line=None, is_jump_target=False), Instruction(opname='MAKE_FUNCTION', opcode=132, optype=None, inst_size=2, arg=0, argval=0, argrepr='', has_arg=True, offset=4, starts_line=None, is_jump_target=False), Instruction(opname='STORE_NAME', opcode=90, optype='name', inst_size=2, arg=0, argval='f', argrepr='f', has_arg=True, offset=6, starts_line=None, is_jump_target=False), Instruction(opname='LOAD_CONST', opcode=100, optype='const', inst_size=2, arg=2, argval=None, argrepr='None', has_arg=True, offset=8, starts_line=None, is_jump_target=False), Instruction(opname='RETURN_VALUE', opcode=83, optype=None, inst_size=2, arg=None, argval=None, argrepr='', has_arg=False, offset=10, starts_line=None, is_jump_target=False)]
>>> code_obj = ops[0]
>>> print(list(dis.Bytecode(code_obj.argval)))
[Instruction(opname='LOAD_CONST', opcode=100, optype='const', inst_size=2, arg=1, argval=1, argrepr='1', has_arg=True, offset=0, starts_line=2, is_jump_target=False), Instruction(opname='RETURN_VALUE', opcode=83, optype=None, inst_size=2, arg=None, argval=None, argrepr='', has_arg=False, offset=2, starts_line=None, is_jump_target=False)]
python dis
New contributor
add a comment |
How to get list of function parameters in python from code object.
import xdis.std as dis
ops = list(dis.Bytecode("""def f(a, b):
return 1"""))
print(ops)
code_obj = ops[0]
print(list(dis.Bytecode(code_obj.argval)))
The above code uses xdis module to decompile code which needs to be installed via pip. However the code object is a normal one which you would expect from the default dis module in python.
I want to know how to get the list of function parameters in order. In this case, a and b
This is the output in interactive python -
>>> import xdis.std as dis
>>> ops = list(dis.Bytecode("""def f(a, b):
... return 1"""))
>>> print(ops)
[Instruction(opname='LOAD_CONST', opcode=100, optype='const', inst_size=2, arg=0, argval=<code object f at 0x7a1a5c4f60, file "<disassembly>", line 1>, argrepr='<code object f at 0x7a1a5c4f60, file "<disassembly>", line 1>', has_arg=True, offset=0, starts_line=1, is_jump_target=False), Instruction(opname='LOAD_CONST', opcode=100, optype='const', inst_size=2, arg=1, argval='f', argrepr="'f'", has_arg=True, offset=2, starts_line=None, is_jump_target=False), Instruction(opname='MAKE_FUNCTION', opcode=132, optype=None, inst_size=2, arg=0, argval=0, argrepr='', has_arg=True, offset=4, starts_line=None, is_jump_target=False), Instruction(opname='STORE_NAME', opcode=90, optype='name', inst_size=2, arg=0, argval='f', argrepr='f', has_arg=True, offset=6, starts_line=None, is_jump_target=False), Instruction(opname='LOAD_CONST', opcode=100, optype='const', inst_size=2, arg=2, argval=None, argrepr='None', has_arg=True, offset=8, starts_line=None, is_jump_target=False), Instruction(opname='RETURN_VALUE', opcode=83, optype=None, inst_size=2, arg=None, argval=None, argrepr='', has_arg=False, offset=10, starts_line=None, is_jump_target=False)]
>>> code_obj = ops[0]
>>> print(list(dis.Bytecode(code_obj.argval)))
[Instruction(opname='LOAD_CONST', opcode=100, optype='const', inst_size=2, arg=1, argval=1, argrepr='1', has_arg=True, offset=0, starts_line=2, is_jump_target=False), Instruction(opname='RETURN_VALUE', opcode=83, optype=None, inst_size=2, arg=None, argval=None, argrepr='', has_arg=False, offset=2, starts_line=None, is_jump_target=False)]
python dis
New contributor
add a comment |
How to get list of function parameters in python from code object.
import xdis.std as dis
ops = list(dis.Bytecode("""def f(a, b):
return 1"""))
print(ops)
code_obj = ops[0]
print(list(dis.Bytecode(code_obj.argval)))
The above code uses xdis module to decompile code which needs to be installed via pip. However the code object is a normal one which you would expect from the default dis module in python.
I want to know how to get the list of function parameters in order. In this case, a and b
This is the output in interactive python -
>>> import xdis.std as dis
>>> ops = list(dis.Bytecode("""def f(a, b):
... return 1"""))
>>> print(ops)
[Instruction(opname='LOAD_CONST', opcode=100, optype='const', inst_size=2, arg=0, argval=<code object f at 0x7a1a5c4f60, file "<disassembly>", line 1>, argrepr='<code object f at 0x7a1a5c4f60, file "<disassembly>", line 1>', has_arg=True, offset=0, starts_line=1, is_jump_target=False), Instruction(opname='LOAD_CONST', opcode=100, optype='const', inst_size=2, arg=1, argval='f', argrepr="'f'", has_arg=True, offset=2, starts_line=None, is_jump_target=False), Instruction(opname='MAKE_FUNCTION', opcode=132, optype=None, inst_size=2, arg=0, argval=0, argrepr='', has_arg=True, offset=4, starts_line=None, is_jump_target=False), Instruction(opname='STORE_NAME', opcode=90, optype='name', inst_size=2, arg=0, argval='f', argrepr='f', has_arg=True, offset=6, starts_line=None, is_jump_target=False), Instruction(opname='LOAD_CONST', opcode=100, optype='const', inst_size=2, arg=2, argval=None, argrepr='None', has_arg=True, offset=8, starts_line=None, is_jump_target=False), Instruction(opname='RETURN_VALUE', opcode=83, optype=None, inst_size=2, arg=None, argval=None, argrepr='', has_arg=False, offset=10, starts_line=None, is_jump_target=False)]
>>> code_obj = ops[0]
>>> print(list(dis.Bytecode(code_obj.argval)))
[Instruction(opname='LOAD_CONST', opcode=100, optype='const', inst_size=2, arg=1, argval=1, argrepr='1', has_arg=True, offset=0, starts_line=2, is_jump_target=False), Instruction(opname='RETURN_VALUE', opcode=83, optype=None, inst_size=2, arg=None, argval=None, argrepr='', has_arg=False, offset=2, starts_line=None, is_jump_target=False)]
python dis
New contributor
How to get list of function parameters in python from code object.
import xdis.std as dis
ops = list(dis.Bytecode("""def f(a, b):
return 1"""))
print(ops)
code_obj = ops[0]
print(list(dis.Bytecode(code_obj.argval)))
The above code uses xdis module to decompile code which needs to be installed via pip. However the code object is a normal one which you would expect from the default dis module in python.
I want to know how to get the list of function parameters in order. In this case, a and b
This is the output in interactive python -
>>> import xdis.std as dis
>>> ops = list(dis.Bytecode("""def f(a, b):
... return 1"""))
>>> print(ops)
[Instruction(opname='LOAD_CONST', opcode=100, optype='const', inst_size=2, arg=0, argval=<code object f at 0x7a1a5c4f60, file "<disassembly>", line 1>, argrepr='<code object f at 0x7a1a5c4f60, file "<disassembly>", line 1>', has_arg=True, offset=0, starts_line=1, is_jump_target=False), Instruction(opname='LOAD_CONST', opcode=100, optype='const', inst_size=2, arg=1, argval='f', argrepr="'f'", has_arg=True, offset=2, starts_line=None, is_jump_target=False), Instruction(opname='MAKE_FUNCTION', opcode=132, optype=None, inst_size=2, arg=0, argval=0, argrepr='', has_arg=True, offset=4, starts_line=None, is_jump_target=False), Instruction(opname='STORE_NAME', opcode=90, optype='name', inst_size=2, arg=0, argval='f', argrepr='f', has_arg=True, offset=6, starts_line=None, is_jump_target=False), Instruction(opname='LOAD_CONST', opcode=100, optype='const', inst_size=2, arg=2, argval=None, argrepr='None', has_arg=True, offset=8, starts_line=None, is_jump_target=False), Instruction(opname='RETURN_VALUE', opcode=83, optype=None, inst_size=2, arg=None, argval=None, argrepr='', has_arg=False, offset=10, starts_line=None, is_jump_target=False)]
>>> code_obj = ops[0]
>>> print(list(dis.Bytecode(code_obj.argval)))
[Instruction(opname='LOAD_CONST', opcode=100, optype='const', inst_size=2, arg=1, argval=1, argrepr='1', has_arg=True, offset=0, starts_line=2, is_jump_target=False), Instruction(opname='RETURN_VALUE', opcode=83, optype=None, inst_size=2, arg=None, argval=None, argrepr='', has_arg=False, offset=2, starts_line=None, is_jump_target=False)]
python dis
python dis
New contributor
New contributor
New contributor
asked Mar 21 at 14:48
ShadowDoomShadowDoom
52
52
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
xdis isn't going to give you much useful functionality here. It's just giving you some more object'd output that you could theoretically muck with a bit easier than the regular dis
module. But, the stock module tells us all we need to know:
>>> from dis import dis
>>> def f(a, b):
... return 1
...
>>> dis(f)
2 0 LOAD_CONST 1 (1)
2 RETURN_VALUE
Note how the disassembly includes only two opcodes. LOAD_CONST
pushes a 1
onto the stack (the cpython runtime is stack based) and RETURN_VALUE
returns from the function with the value on the top of the stack. There is no mention of a
nor b
here. And this makes sense. They aren't used! Byte the byte code doesn't concern itself with function arguments. It will emit the necessary ops to put them on the stack (where needed):
>>> def f(a, b):
... return a + b
...
>>> dis(f)
2 0 LOAD_FAST 0 (a)
2 LOAD_FAST 1 (b)
4 BINARY_ADD
6 RETURN_VALUE
Note here that LOAD_FAST
gets a
and b
and pushes them onto the stack for BINARY_ADD
(which adds the top two values on the stack and pushes the result).
You can get at what you want by using __code__
, specifically:
params_and_locals = f.__code__.co_varnames
num_args = f.__code__.co_argcount + f.__code__.co_kwonlyargcount
params = params_and_locals[:num_args]
add a comment |
If you have a code object
def f(a, b, *, c=True):
e = a + b
if c:
return a * e
code_obj = f.__code__
then the positional parameters are
code_obj.co_varnames[:code_obj.co_argcount]
# --> ('a', 'b')
and the keyword only parameters are
code_obj.co_varnames[code_obj.co_argcount : code_obj.co_argcount + code_obj.co_kwonlyargcount]
# --> ('c',)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
ShadowDoom is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55283169%2fhow-to-get-function-parameters-from-dis%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
xdis isn't going to give you much useful functionality here. It's just giving you some more object'd output that you could theoretically muck with a bit easier than the regular dis
module. But, the stock module tells us all we need to know:
>>> from dis import dis
>>> def f(a, b):
... return 1
...
>>> dis(f)
2 0 LOAD_CONST 1 (1)
2 RETURN_VALUE
Note how the disassembly includes only two opcodes. LOAD_CONST
pushes a 1
onto the stack (the cpython runtime is stack based) and RETURN_VALUE
returns from the function with the value on the top of the stack. There is no mention of a
nor b
here. And this makes sense. They aren't used! Byte the byte code doesn't concern itself with function arguments. It will emit the necessary ops to put them on the stack (where needed):
>>> def f(a, b):
... return a + b
...
>>> dis(f)
2 0 LOAD_FAST 0 (a)
2 LOAD_FAST 1 (b)
4 BINARY_ADD
6 RETURN_VALUE
Note here that LOAD_FAST
gets a
and b
and pushes them onto the stack for BINARY_ADD
(which adds the top two values on the stack and pushes the result).
You can get at what you want by using __code__
, specifically:
params_and_locals = f.__code__.co_varnames
num_args = f.__code__.co_argcount + f.__code__.co_kwonlyargcount
params = params_and_locals[:num_args]
add a comment |
xdis isn't going to give you much useful functionality here. It's just giving you some more object'd output that you could theoretically muck with a bit easier than the regular dis
module. But, the stock module tells us all we need to know:
>>> from dis import dis
>>> def f(a, b):
... return 1
...
>>> dis(f)
2 0 LOAD_CONST 1 (1)
2 RETURN_VALUE
Note how the disassembly includes only two opcodes. LOAD_CONST
pushes a 1
onto the stack (the cpython runtime is stack based) and RETURN_VALUE
returns from the function with the value on the top of the stack. There is no mention of a
nor b
here. And this makes sense. They aren't used! Byte the byte code doesn't concern itself with function arguments. It will emit the necessary ops to put them on the stack (where needed):
>>> def f(a, b):
... return a + b
...
>>> dis(f)
2 0 LOAD_FAST 0 (a)
2 LOAD_FAST 1 (b)
4 BINARY_ADD
6 RETURN_VALUE
Note here that LOAD_FAST
gets a
and b
and pushes them onto the stack for BINARY_ADD
(which adds the top two values on the stack and pushes the result).
You can get at what you want by using __code__
, specifically:
params_and_locals = f.__code__.co_varnames
num_args = f.__code__.co_argcount + f.__code__.co_kwonlyargcount
params = params_and_locals[:num_args]
add a comment |
xdis isn't going to give you much useful functionality here. It's just giving you some more object'd output that you could theoretically muck with a bit easier than the regular dis
module. But, the stock module tells us all we need to know:
>>> from dis import dis
>>> def f(a, b):
... return 1
...
>>> dis(f)
2 0 LOAD_CONST 1 (1)
2 RETURN_VALUE
Note how the disassembly includes only two opcodes. LOAD_CONST
pushes a 1
onto the stack (the cpython runtime is stack based) and RETURN_VALUE
returns from the function with the value on the top of the stack. There is no mention of a
nor b
here. And this makes sense. They aren't used! Byte the byte code doesn't concern itself with function arguments. It will emit the necessary ops to put them on the stack (where needed):
>>> def f(a, b):
... return a + b
...
>>> dis(f)
2 0 LOAD_FAST 0 (a)
2 LOAD_FAST 1 (b)
4 BINARY_ADD
6 RETURN_VALUE
Note here that LOAD_FAST
gets a
and b
and pushes them onto the stack for BINARY_ADD
(which adds the top two values on the stack and pushes the result).
You can get at what you want by using __code__
, specifically:
params_and_locals = f.__code__.co_varnames
num_args = f.__code__.co_argcount + f.__code__.co_kwonlyargcount
params = params_and_locals[:num_args]
xdis isn't going to give you much useful functionality here. It's just giving you some more object'd output that you could theoretically muck with a bit easier than the regular dis
module. But, the stock module tells us all we need to know:
>>> from dis import dis
>>> def f(a, b):
... return 1
...
>>> dis(f)
2 0 LOAD_CONST 1 (1)
2 RETURN_VALUE
Note how the disassembly includes only two opcodes. LOAD_CONST
pushes a 1
onto the stack (the cpython runtime is stack based) and RETURN_VALUE
returns from the function with the value on the top of the stack. There is no mention of a
nor b
here. And this makes sense. They aren't used! Byte the byte code doesn't concern itself with function arguments. It will emit the necessary ops to put them on the stack (where needed):
>>> def f(a, b):
... return a + b
...
>>> dis(f)
2 0 LOAD_FAST 0 (a)
2 LOAD_FAST 1 (b)
4 BINARY_ADD
6 RETURN_VALUE
Note here that LOAD_FAST
gets a
and b
and pushes them onto the stack for BINARY_ADD
(which adds the top two values on the stack and pushes the result).
You can get at what you want by using __code__
, specifically:
params_and_locals = f.__code__.co_varnames
num_args = f.__code__.co_argcount + f.__code__.co_kwonlyargcount
params = params_and_locals[:num_args]
edited Mar 21 at 15:15
answered Mar 21 at 14:59
Bailey ParkerBailey Parker
12k23871
12k23871
add a comment |
add a comment |
If you have a code object
def f(a, b, *, c=True):
e = a + b
if c:
return a * e
code_obj = f.__code__
then the positional parameters are
code_obj.co_varnames[:code_obj.co_argcount]
# --> ('a', 'b')
and the keyword only parameters are
code_obj.co_varnames[code_obj.co_argcount : code_obj.co_argcount + code_obj.co_kwonlyargcount]
# --> ('c',)
add a comment |
If you have a code object
def f(a, b, *, c=True):
e = a + b
if c:
return a * e
code_obj = f.__code__
then the positional parameters are
code_obj.co_varnames[:code_obj.co_argcount]
# --> ('a', 'b')
and the keyword only parameters are
code_obj.co_varnames[code_obj.co_argcount : code_obj.co_argcount + code_obj.co_kwonlyargcount]
# --> ('c',)
add a comment |
If you have a code object
def f(a, b, *, c=True):
e = a + b
if c:
return a * e
code_obj = f.__code__
then the positional parameters are
code_obj.co_varnames[:code_obj.co_argcount]
# --> ('a', 'b')
and the keyword only parameters are
code_obj.co_varnames[code_obj.co_argcount : code_obj.co_argcount + code_obj.co_kwonlyargcount]
# --> ('c',)
If you have a code object
def f(a, b, *, c=True):
e = a + b
if c:
return a * e
code_obj = f.__code__
then the positional parameters are
code_obj.co_varnames[:code_obj.co_argcount]
# --> ('a', 'b')
and the keyword only parameters are
code_obj.co_varnames[code_obj.co_argcount : code_obj.co_argcount + code_obj.co_kwonlyargcount]
# --> ('c',)
answered Mar 21 at 14:56
FHTMitchellFHTMitchell
8,24411229
8,24411229
add a comment |
add a comment |
ShadowDoom is a new contributor. Be nice, and check out our Code of Conduct.
ShadowDoom is a new contributor. Be nice, and check out our Code of Conduct.
ShadowDoom is a new contributor. Be nice, and check out our Code of Conduct.
ShadowDoom is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55283169%2fhow-to-get-function-parameters-from-dis%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown