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

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

    However, I would also like it to pass the output as a list

    You mean "return the output as a dictionary" - be careful ;-)

    One thing you could do is use the ability of the Python interpreter to automatically convert to a string the result of any expression. To do this, create a custom subclass of dict that, when asked to convert itself to a string, performs whatever pretty formatting you want. For instance,

    class PrettyDict(dict):
        def __str__(self):
            return '''Pretty printer status check 0.02v
    NOTE: This is so totally not written for giant robots
    =================================
    System operational: ... %s
    Time to ion canon charge is %dm %ds
    Booster rocket in %s state
     (other stuff)
    ''' % (self.conf_op and 'ok' or 'OMGPANIC!!!',
           self.t_canoncharge / 60, self.t_canoncharge % 60, 
           BOOSTER_STATES[self.booster_charge],
           ... )
    

    Of course you could probably come up with a prettier way to write the code, but the basic idea is that the __str__ method creates the pretty-printed string that represents the state of the object and returns it. Then, if you return a PrettyDict from your check_status() function, when you type

    >>> check_status()
    

    you would see

    Pretty printer status check 0.02v
    NOTE: This is so totally not written for giant robots
    =================================
    System operational: ... ok
    Time to ion canon charge is 9m 21s
    Booster rocket in AFTERBURNER state
    Range check is optimal
    Rocket fuel is 10h 19m 40s to depletion
    Beer served is type WICKSE LAGER, chill optimal
    Suggested catchphrase is 01_FIGHTING_SPIRIT_GOGOGO
    Virtual ... on

    The only catch is that

    >>> not_robot_stat = check_status()
    >>> print not_robot_stat
    

    would give you the same thing, because the same conversion to string takes place as part of the print function. But for using this in some real application, I doubt that that would matter. If you really wanted to see the return value as a pure dict, you could do

    >>> print repr(not_robot_stat)
    

    instead, and it should show you

    {'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'}

    The point is that, as other posters have said, there is no way for a function in Python to know what's going to be done with it's return value (EDIT: okay, maybe there would be some weird bytecode-hacking way, but don't do that) - but you can work around it in the cases that matter.

提交回复
热议问题