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

前端 未结 7 983
[愿得一人]
[愿得一人] 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:12

    Even if there is a way to do this, it's a bad idea. Imagine trying to debug something that behaves differently depending on the context it's called from. Now try to imagine that it's six months from now and this is buried in part of some system that's too big to keep in your head all at once.

    Keep it simple. Explicit is better than implicit. Just make a pretty-print function (or use the pprint module) and call that on the result. In an interactive Pythn session you can use _ to get the value of the last expression.

提交回复
热议问题