Is there a way to check whether function output is assigned to a variable in Python?

前端 未结 7 989
[愿得一人]
[愿得一人] 2021-01-22 14:31

In Python, I\'d like to write a function that would pretty-print its results to the console if called by itself (mostly for use interactively or for debugging). For the purpose

7条回答
  •  既然无缘
    2021-01-22 15:10

    The only way to do what you want is indeed "to play with the bytecode" -- there is no other way to recover that info. A much better way, of course, is to provide two separate functions: one to get the status as a dict, the other to call the first one and format it.

    Alternatively (but not an excellent architecture) you could have a single function that takes an optional parameter to behave in these two utterly different ways -- this is not excellent because a function should have ONE function, i.e. basically do ONE thing, not two different ones such as these.

提交回复
热议问题