Exploring code objectsFactorisation code running slowDumping fields from Python objectsBuilding Data abstraction for line segments using “objects”HackerRank “Lists” codeGeneric number class codeFirst-class objects in PythonDownset calculation codeWrite/read efficiently dataframe objects into memory or diskTell Me More Effecient Way For This CodePython code issues
Why "Having chlorophyll without photosynthesis is actually very dangerous" and "like living with a bomb"?
Rock identification in KY
Should the isomorphism theorems be seen as an "interface" between algebra and category theory?
Today is the Center
Theorems that impeded progress
Is it unprofessional to ask if a job posting on GlassDoor is real?
Alternative to sending password over mail?
Revoked SSL certificate
A newer friend of my brother's gave him a load of baseball cards that are supposedly extremely valuable. Is this a scam?
What's that red-plus icon near a text?
Do infinite dimensional systems make sense?
How do I deal with an unproductive colleague in a small company?
Paid for article while in US on F1 visa?
What does it mean to describe someone as a butt steak?
Was any UN Security Council vote triple-vetoed?
Arithmetic with LuaLaTeX
I'm flying to France today and my passport expires in less than 2 months
dbcc cleantable batch size explanation
"You are your self first supporter", a more proper way to say it
Maximum likelihood parameters deviate from posterior distributions
Replacing matching entries in one column of a file by another column from a different file
Accidentally leaked the solution to an assignment, what to do now? (I'm the prof)
How to regain access to running applications after accidentally zapping X.org?
Operational amplifier as a comparator at high frequency
Exploring code objects
Factorisation code running slowDumping fields from Python objectsBuilding Data abstraction for line segments using “objects”HackerRank “Lists” codeGeneric number class codeFirst-class objects in PythonDownset calculation codeWrite/read efficiently dataframe objects into memory or diskTell Me More Effecient Way For This CodePython code issues
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
I am reading the book: "Obi Ike-Nwosu. Inside The Python Virtual Machine" and have reached the "Code Objects" chapter. This chapter has all code object fields printed out:
co_argcount = 1
co_cellvars = ()
co_code = b'|x00dx01x16x00dx02kx02rx1e|x00dx03x16x00dx02kx02rx1ed\
x04Sx00n,|x00dx01x16x00dx02kx02r0dx05Sx00nx1a|x00dx03x16x00dx02kx02r
Bdx06Sx00nx08tx00|x00x83x01Sx00dx00Sx00'
co_consts = (None, 3, 0, 5, 'FizzBuzz', 'Fizz', 'Buzz')
co_filename = /Users/c4obi/projects/python_source/cpython/fizzbuzz.py
co_firstlineno = 6
co_flags = 67
etc...
but there is no explanation how this can be done programatically, so I wrote my own code:
# Making the code object for 'my_add' function
code_obj = compile('my_add', os.path.realpath(__file__), 'exec')
# Iterating through all instance attributes
# and calling all having the 'co_' prefix
for name in dir(code_obj):
if name.startswith('co_'):
co_field = getattr(code_obj, name)
print(f'name:<20 = co_field')
Output
co_argcount = 0
co_cellvars = ()
co_code = b'ex00x01x00dx00Sx00'
co_consts = (None,)
co_filename = /home/minimax/learning_python/oop/source.py
co_firstlineno = 1
co_flags = 64
co_freevars = ()
co_kwonlyargcount = 0
co_lnotab = b''
co_name = <module>
co_names = ('my_add',)
co_nlocals = 0
co_stacksize = 1
co_varnames = ()
Question: Have I used the optimal way? How would you solve this problem?
python-3.x
$endgroup$
add a comment |
$begingroup$
I am reading the book: "Obi Ike-Nwosu. Inside The Python Virtual Machine" and have reached the "Code Objects" chapter. This chapter has all code object fields printed out:
co_argcount = 1
co_cellvars = ()
co_code = b'|x00dx01x16x00dx02kx02rx1e|x00dx03x16x00dx02kx02rx1ed\
x04Sx00n,|x00dx01x16x00dx02kx02r0dx05Sx00nx1a|x00dx03x16x00dx02kx02r
Bdx06Sx00nx08tx00|x00x83x01Sx00dx00Sx00'
co_consts = (None, 3, 0, 5, 'FizzBuzz', 'Fizz', 'Buzz')
co_filename = /Users/c4obi/projects/python_source/cpython/fizzbuzz.py
co_firstlineno = 6
co_flags = 67
etc...
but there is no explanation how this can be done programatically, so I wrote my own code:
# Making the code object for 'my_add' function
code_obj = compile('my_add', os.path.realpath(__file__), 'exec')
# Iterating through all instance attributes
# and calling all having the 'co_' prefix
for name in dir(code_obj):
if name.startswith('co_'):
co_field = getattr(code_obj, name)
print(f'name:<20 = co_field')
Output
co_argcount = 0
co_cellvars = ()
co_code = b'ex00x01x00dx00Sx00'
co_consts = (None,)
co_filename = /home/minimax/learning_python/oop/source.py
co_firstlineno = 1
co_flags = 64
co_freevars = ()
co_kwonlyargcount = 0
co_lnotab = b''
co_name = <module>
co_names = ('my_add',)
co_nlocals = 0
co_stacksize = 1
co_varnames = ()
Question: Have I used the optimal way? How would you solve this problem?
python-3.x
$endgroup$
add a comment |
$begingroup$
I am reading the book: "Obi Ike-Nwosu. Inside The Python Virtual Machine" and have reached the "Code Objects" chapter. This chapter has all code object fields printed out:
co_argcount = 1
co_cellvars = ()
co_code = b'|x00dx01x16x00dx02kx02rx1e|x00dx03x16x00dx02kx02rx1ed\
x04Sx00n,|x00dx01x16x00dx02kx02r0dx05Sx00nx1a|x00dx03x16x00dx02kx02r
Bdx06Sx00nx08tx00|x00x83x01Sx00dx00Sx00'
co_consts = (None, 3, 0, 5, 'FizzBuzz', 'Fizz', 'Buzz')
co_filename = /Users/c4obi/projects/python_source/cpython/fizzbuzz.py
co_firstlineno = 6
co_flags = 67
etc...
but there is no explanation how this can be done programatically, so I wrote my own code:
# Making the code object for 'my_add' function
code_obj = compile('my_add', os.path.realpath(__file__), 'exec')
# Iterating through all instance attributes
# and calling all having the 'co_' prefix
for name in dir(code_obj):
if name.startswith('co_'):
co_field = getattr(code_obj, name)
print(f'name:<20 = co_field')
Output
co_argcount = 0
co_cellvars = ()
co_code = b'ex00x01x00dx00Sx00'
co_consts = (None,)
co_filename = /home/minimax/learning_python/oop/source.py
co_firstlineno = 1
co_flags = 64
co_freevars = ()
co_kwonlyargcount = 0
co_lnotab = b''
co_name = <module>
co_names = ('my_add',)
co_nlocals = 0
co_stacksize = 1
co_varnames = ()
Question: Have I used the optimal way? How would you solve this problem?
python-3.x
$endgroup$
I am reading the book: "Obi Ike-Nwosu. Inside The Python Virtual Machine" and have reached the "Code Objects" chapter. This chapter has all code object fields printed out:
co_argcount = 1
co_cellvars = ()
co_code = b'|x00dx01x16x00dx02kx02rx1e|x00dx03x16x00dx02kx02rx1ed\
x04Sx00n,|x00dx01x16x00dx02kx02r0dx05Sx00nx1a|x00dx03x16x00dx02kx02r
Bdx06Sx00nx08tx00|x00x83x01Sx00dx00Sx00'
co_consts = (None, 3, 0, 5, 'FizzBuzz', 'Fizz', 'Buzz')
co_filename = /Users/c4obi/projects/python_source/cpython/fizzbuzz.py
co_firstlineno = 6
co_flags = 67
etc...
but there is no explanation how this can be done programatically, so I wrote my own code:
# Making the code object for 'my_add' function
code_obj = compile('my_add', os.path.realpath(__file__), 'exec')
# Iterating through all instance attributes
# and calling all having the 'co_' prefix
for name in dir(code_obj):
if name.startswith('co_'):
co_field = getattr(code_obj, name)
print(f'name:<20 = co_field')
Output
co_argcount = 0
co_cellvars = ()
co_code = b'ex00x01x00dx00Sx00'
co_consts = (None,)
co_filename = /home/minimax/learning_python/oop/source.py
co_firstlineno = 1
co_flags = 64
co_freevars = ()
co_kwonlyargcount = 0
co_lnotab = b''
co_name = <module>
co_names = ('my_add',)
co_nlocals = 0
co_stacksize = 1
co_varnames = ()
Question: Have I used the optimal way? How would you solve this problem?
python-3.x
python-3.x
asked 13 mins ago
MiniMaxMiniMax
1494
1494
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");
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: "196"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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%2fcodereview.stackexchange.com%2fquestions%2f216944%2fexploring-code-objects%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Code Review Stack Exchange!
- 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.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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%2fcodereview.stackexchange.com%2fquestions%2f216944%2fexploring-code-objects%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');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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