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

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

    There's no use case for this. Python assigns all interactive results to a special variable named _.

    You can do the following interactively. Works great. No funny business.

    >>> check_status()
    {'cond_op': 1, 't_canoncharge': 1342, 'stage_booster': 5, 'range_est_sigma': 0.023, 'fuel_est': 32557154, 'beer_type': 31007, 'beer_temp': 2, 'catchphrase_suggestion': 1023, 'virtual_on': 'hell yes'}
    
    >>> pprint.pprint( _ )
    {'beer_temp': 2,
     'beer_type': 31007,
     'catchphrase_suggestion': 1023,
     'cond_op': 1,
     'fuel_est': 32557154,
     'range_est_sigma': 0.023,
     'stage_booster': 5,
     't_canoncharge': 1342,
     'virtual_on': 'hell yes'}
    

提交回复
热议问题