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


In Python, I would like to write a function that beautifully prints its results in the console if it is called itself (mostly For interactive use or for debugging). For the purpose of this question, suppose it checks the status of someone if I just call

  check_status ()  

let me do this To see something:

  Examine the beautiful printer status 0.02v Note: This is not written entirely for huge robots =============== ================ System Operations: .. fine ion canon charging time is optimum rocket fuel 10h 19m 40 shortage has served beer in the afterburner state range check in the 9 m-21 booster rocket type wickle beer, chill is optimal recommended cushion Kalam 01_FIGHTING_SPIRIT_GOGOGO virtual ... However, I also like that this output will be in the form of a list if I pass a variable called it in terms of work:  
 < Code> not_robot_stat = check_status () not_robot_stat print & gt; & Gt; & Gt; {'Cond_op': 1, 't_canoncharge': 1342, 'stage_booster': 5, 'range_est_sigma': 0.023, 'fuel_est': 32,557,154, 'beer_type': 31,007, 'beer_temp': 2, 'catchphrase_suggestion': 1023, Virtual_on ':' hell yeah '}  

So ... is there any way to dynamically detect within a function, whether its output is being assigned? I would like to be able to do this to pass the ultimate, or to write another function dedicated to it. I have scored for a while, and I can tell it a little bit like I like to play a byte cock. Is this really necessary?

new solution

this is a new way Detects the solution that the result of the function is used for assignment, so that its own bitcodes can be detected. No bytecode has been written, and it should also be compatible with the future versions of Python because it uses the OPOD module for definitions.

  import inspection, disc, opcode def check_status (): try:. Frame = inspect.currentframe () f_back next_opcode = opcode.opname [ord (frame.f_code.co_code [frame.f_lasti + 3])] if next_opcode == "POP_TOP": # or next_opcode == "RETURN_VALUE": #included if If you consider printing assignments on "Return check_States ()", then the above line "I was not printed." Check the beauty of the printer 0.02v "Print" Note: This is not completely written for Giant Robots "Return to end: Del frame # Normal routine information = {'cond_op': 1, 't_canoncharge': 1342, 'stage_booster': 5} do flame The information # no work DEF test1 (): check_status (): #def test2 (): A = Check_status () # could be a Ssignment (check for option above) def test3 (): return check_status () # Assignment def test4 (): a = [] a.append (check_status ()) A  

Solution 1

This is an old solution That is, whenever you call a function while debugging under python-i or PDB, it detects.

  Import def_view Inspect the stratus (): Frame = inspect.currentframe ( ) Try: if frame.f_back.f_code.co_name == " & Lt; Module & gt; "and frame.f_back.f_code.co_filename ==" and lieutenant; Stdin & gt; "Print" Pretty Printer Status 0.02v Check "print" Note: This is not completely written for Giant Robots "Finally: Dell Frame # Return the luggage regularly {'Cond_op': 1, 't_canoncharge': 1342, 'step_boster': 5} def test (): check_status ()> gt>> check_status () Pretty printer status check 0.02v Note: Robot {: 1, 't_canoncharge': 1342, 'stage_booster': not written for 'cond_op' 5};>> A = check_status () Pretty printer position 0.02 Check v. Note: It has not been written for a fully enormous robot>> 'cond_op': 1, 't_canoncharge': 1342, 'stage_boster': 5} exam () > Gt; & gt; & gt;  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -