What's the best why of dumping the location of a Python function/method?












-1














I'm working on a framework that takes a python function/classmethod/staticmethod as its input, then execute the given one remotely.



How could I dump the full location of a python function? It would be fairly easy if the function is at the top level, then I'll only need to save the module name and the function name then retrieve the function by getattr.



So far as I consider the nested functions such as the staticmethod, python doesn't maintain the connection between the method and its container. Therefore you lose part of the information, and if you still would like to use the getattr mechanism, there will be a nested loop for searching all nested objects.



I'm thinking of the co_firstlineno for the extra clue of retrieving the function object later, but I didn't find a proper way of getting the function object from the module by line. I even tried to dump the code object through marshal, but reassembling the function object from the code object would be a nightmare.










share|improve this question
























  • You should take a look at the inspect module of the standard library: docs.python.org/3/library/inspect.html
    – Vladimir Poghosyan
    Nov 26 at 18:42










  • Hey @VladimirPoghosyan, thanks for your response. I've checked the inspect module at the very beginning, but it doesn't seem to be like the solution. The module is all about wrapping the methods of FunctionType and CodeType, which lose this feature internally. The same thing happens in the pickle module as well. You cannot pickle a static method since it cannot find the method from its module.
    – Calvin
    Nov 27 at 21:13










  • Note that the Python standard library pickle has to solve the same problem *and doesn’t support anything beyond base module.name references. Take a look at how / what the dill project does; it extends pickle beyond module.name references.
    – Martijn Pieters
    Dec 8 at 5:47
















-1














I'm working on a framework that takes a python function/classmethod/staticmethod as its input, then execute the given one remotely.



How could I dump the full location of a python function? It would be fairly easy if the function is at the top level, then I'll only need to save the module name and the function name then retrieve the function by getattr.



So far as I consider the nested functions such as the staticmethod, python doesn't maintain the connection between the method and its container. Therefore you lose part of the information, and if you still would like to use the getattr mechanism, there will be a nested loop for searching all nested objects.



I'm thinking of the co_firstlineno for the extra clue of retrieving the function object later, but I didn't find a proper way of getting the function object from the module by line. I even tried to dump the code object through marshal, but reassembling the function object from the code object would be a nightmare.










share|improve this question
























  • You should take a look at the inspect module of the standard library: docs.python.org/3/library/inspect.html
    – Vladimir Poghosyan
    Nov 26 at 18:42










  • Hey @VladimirPoghosyan, thanks for your response. I've checked the inspect module at the very beginning, but it doesn't seem to be like the solution. The module is all about wrapping the methods of FunctionType and CodeType, which lose this feature internally. The same thing happens in the pickle module as well. You cannot pickle a static method since it cannot find the method from its module.
    – Calvin
    Nov 27 at 21:13










  • Note that the Python standard library pickle has to solve the same problem *and doesn’t support anything beyond base module.name references. Take a look at how / what the dill project does; it extends pickle beyond module.name references.
    – Martijn Pieters
    Dec 8 at 5:47














-1












-1








-1







I'm working on a framework that takes a python function/classmethod/staticmethod as its input, then execute the given one remotely.



How could I dump the full location of a python function? It would be fairly easy if the function is at the top level, then I'll only need to save the module name and the function name then retrieve the function by getattr.



So far as I consider the nested functions such as the staticmethod, python doesn't maintain the connection between the method and its container. Therefore you lose part of the information, and if you still would like to use the getattr mechanism, there will be a nested loop for searching all nested objects.



I'm thinking of the co_firstlineno for the extra clue of retrieving the function object later, but I didn't find a proper way of getting the function object from the module by line. I even tried to dump the code object through marshal, but reassembling the function object from the code object would be a nightmare.










share|improve this question















I'm working on a framework that takes a python function/classmethod/staticmethod as its input, then execute the given one remotely.



How could I dump the full location of a python function? It would be fairly easy if the function is at the top level, then I'll only need to save the module name and the function name then retrieve the function by getattr.



So far as I consider the nested functions such as the staticmethod, python doesn't maintain the connection between the method and its container. Therefore you lose part of the information, and if you still would like to use the getattr mechanism, there will be a nested loop for searching all nested objects.



I'm thinking of the co_firstlineno for the extra clue of retrieving the function object later, but I didn't find a proper way of getting the function object from the module by line. I even tried to dump the code object through marshal, but reassembling the function object from the code object would be a nightmare.







python serialization cpython python-internals






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 22:41









jonrsharpe

76.7k11100207




76.7k11100207










asked Nov 22 at 22:14









Calvin

215




215












  • You should take a look at the inspect module of the standard library: docs.python.org/3/library/inspect.html
    – Vladimir Poghosyan
    Nov 26 at 18:42










  • Hey @VladimirPoghosyan, thanks for your response. I've checked the inspect module at the very beginning, but it doesn't seem to be like the solution. The module is all about wrapping the methods of FunctionType and CodeType, which lose this feature internally. The same thing happens in the pickle module as well. You cannot pickle a static method since it cannot find the method from its module.
    – Calvin
    Nov 27 at 21:13










  • Note that the Python standard library pickle has to solve the same problem *and doesn’t support anything beyond base module.name references. Take a look at how / what the dill project does; it extends pickle beyond module.name references.
    – Martijn Pieters
    Dec 8 at 5:47


















  • You should take a look at the inspect module of the standard library: docs.python.org/3/library/inspect.html
    – Vladimir Poghosyan
    Nov 26 at 18:42










  • Hey @VladimirPoghosyan, thanks for your response. I've checked the inspect module at the very beginning, but it doesn't seem to be like the solution. The module is all about wrapping the methods of FunctionType and CodeType, which lose this feature internally. The same thing happens in the pickle module as well. You cannot pickle a static method since it cannot find the method from its module.
    – Calvin
    Nov 27 at 21:13










  • Note that the Python standard library pickle has to solve the same problem *and doesn’t support anything beyond base module.name references. Take a look at how / what the dill project does; it extends pickle beyond module.name references.
    – Martijn Pieters
    Dec 8 at 5:47
















You should take a look at the inspect module of the standard library: docs.python.org/3/library/inspect.html
– Vladimir Poghosyan
Nov 26 at 18:42




You should take a look at the inspect module of the standard library: docs.python.org/3/library/inspect.html
– Vladimir Poghosyan
Nov 26 at 18:42












Hey @VladimirPoghosyan, thanks for your response. I've checked the inspect module at the very beginning, but it doesn't seem to be like the solution. The module is all about wrapping the methods of FunctionType and CodeType, which lose this feature internally. The same thing happens in the pickle module as well. You cannot pickle a static method since it cannot find the method from its module.
– Calvin
Nov 27 at 21:13




Hey @VladimirPoghosyan, thanks for your response. I've checked the inspect module at the very beginning, but it doesn't seem to be like the solution. The module is all about wrapping the methods of FunctionType and CodeType, which lose this feature internally. The same thing happens in the pickle module as well. You cannot pickle a static method since it cannot find the method from its module.
– Calvin
Nov 27 at 21:13












Note that the Python standard library pickle has to solve the same problem *and doesn’t support anything beyond base module.name references. Take a look at how / what the dill project does; it extends pickle beyond module.name references.
– Martijn Pieters
Dec 8 at 5:47




Note that the Python standard library pickle has to solve the same problem *and doesn’t support anything beyond base module.name references. Take a look at how / what the dill project does; it extends pickle beyond module.name references.
– Martijn Pieters
Dec 8 at 5:47

















active

oldest

votes











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438553%2fwhats-the-best-why-of-dumping-the-location-of-a-python-function-method%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438553%2fwhats-the-best-why-of-dumping-the-location-of-a-python-function-method%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks