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

8

3

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 of this question, let's say it checks the status of something. If I call just

check_status()

I would like to see something like:

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

However, I would also like it to pass the output as a list if I call it in the context of a variable assignment:

not_robot_stat = check_status()
print not_robot_stat
>>> {'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'}

So... is there a way to dynamically know, within a function, whether its output is being assigned? I'd like to be able to do this without resorting param passing, or writing another function dedicated for this. I've Googled for a bit, and from what little I can tell it looks like I'd have to resort to playing wth the bytecode. Is that really necessary?

Birchard answered 2/5, 2009 at 1:8 Comment(4)
Tim, did you try my code? It will always detect if you are calling the function from debugging and be able to output the diagnostic like you requested.Birdcage
Well I'm going over the feature requirements again to see if this is needed... but looks like I need it to work in ALL cases, not just in interactive mode. The sotry is a bit involved, so I'll try to make it more coherent and update my question in a few days.Birchard
@ Tim, in that case, I have made a new solution using bytecode reading.Birdcage
@Unknown: It's on the right track, but look below for commentsBirchard
B
6

New Solution

This is a new that solution detects when the result of the function is used for assignment by examining its own bytecode. There is no bytecode writing done, and it should even be compatible with future versions of Python because it uses the opcode module for definitions.

import inspect, dis, 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":
            # include the above line in the if statement if you consider "return check_status()" to be assignment
            print "I was not assigned"
            print "Pretty printer status check 0.02v"
            print "NOTE: This is so totally not written for giant robots"
            return
    finally:
        del frame    

    # do normal routine

    info = {'cond_op': 1, 't_canoncharge': 1342, 'stage_booster': 5}

    return info

# no assignment    
def test1():
    check_status()

# assignment
def test2():
    a = check_status()

# could be assignment (check above for options)
def test3():
    return check_status()

# assignment
def test4():
    a = []
    a.append(check_status())
    return a

Solution 1

This is the old solution that detects whenever you are calling the function while debugging under python -i or PDB.

import inspect

def check_status():
    frame = inspect.currentframe()
    try:
        if frame.f_back.f_code.co_name == "<module>" and frame.f_back.f_code.co_filename == "<stdin>":
            print "Pretty printer status check 0.02v"
            print "NOTE: This is so totally not written for giant robots"
    finally:
        del frame

    # do regular stuff   
    return {'cond_op': 1, 't_canoncharge': 1342, 'stage_booster': 5}

def test():
    check_status()


>>> check_status()
Pretty printer status check 0.02v
NOTE: This is so totally not written for giant robots
{'cond_op': 1, 't_canoncharge': 1342, 'stage_booster': 5}

>>> a=check_status()
Pretty printer status check 0.02v
NOTE: This is so totally not written for giant robots

>>> a
{'cond_op': 1, 't_canoncharge': 1342, 'stage_booster': 5}

test()
>>>
Birdcage answered 2/5, 2009 at 1:35 Comment(12)
Thanks! I was hoping for a good bytecode mangling implementation if there's no native syntax way, and this nails it!Birchard
Well actually now that I have had time to look at it closely, it appears that you're still not really distinguishing whether the function was called by itself or called for assignment.Birchard
Counterexample: put the code in a file "blahblah.txt" and run "cat blahblah.txt | python" ;-) but that's just being pedantic. The point is, this solution does not achieve the desired goals of pretty-printing the dictionary or detecting what is being done with the function's return value. (For what it's worth, you seem more a bit desperate - you don't get upvotes by begging for them)Ergonomics
@ David, this code covers cases where you are debugging with python -i and with PDB like you are supposed to.Birdcage
I'm not sure what the point of your last comment is, but I still maintain that your first (original) code sample doesn't do what was asked. Your second code sample is more along the right lines, though. I might have upvoted this if you'd been a bit more gracious in your previous comments.Ergonomics
@ David, so you are going to downvote me because you don't like my comments rather than the validity of the answer?Birdcage
@Unknown: First of all thanks for your effort! This is definitely along the track of what I was looking for. However, on my installation (Python 2.5) the "next_opcode" always evaluates to UNPACK_SEQUENCE no matter whether it's unassigned, or assigned to 1....n variables. I don't know whether this is some problem with my Python. You obviously have played with bytecode though... can you recommend some references where I can learn more about the Python bytecode?Birchard
@Unknown: oh I figured out what was wrong... it was quite a contrived logic bug in the code. I'm gonna mark this as the right solution. Thanks a lot! I would still appreciate some recommendations on bytecode references though.Birchard
@Tim, the problem is that there is not much documentation of the bytecode anywhere. I had to figure it out myself. Take a look at docs.python.org/library/dis.html and the semi-undocumented C:\python25\Lib\opcode.pyBirdcage
@Birdcage any ideas on how to do it now? Documentation seems to be similar, but code does not work...Ocean
FWIW, I got this to work on python3.6 using next_opcode = dis.opname[frame.f_code.co_code[frame.f_lasti + 2]]. I'm not sure why the constant has drifted by 1 or if that is reliable.Eydie
This doesn't work interactively, because POP_TOP is the wrong opcode in interactive mode. Interactive use is probably the most important time for it to work. Also, it isn't compatible with new Python versions due to changes in the bytecode format, and it's incompatible with being called from functions written in C.Elbaelbart
E
4

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.

Ergonomics answered 2/5, 2009 at 1:32 Comment(1)
This is a good suggestion as well... I'd consider it. Thanks!Birchard
C
3

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'}
Coddle answered 2/5, 2009 at 1:42 Comment(0)
S
2

There is no way for a function to know how its return value is being used.

Well, as you mention, egregious bytecode hacks might be able to do it, but it would be really complicated and probably fragile. Go the simpler explicit route.

Sanious answered 2/5, 2009 at 1:15 Comment(0)
B
2

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.

Baedeker answered 2/5, 2009 at 5:1 Comment(0)
S
0

There is no way to do this, at least not with the normal syntax procedures, because the function call and the assignment are completely independent operations, which have no awareness of each other.

The simplest workaround I can see for this is to pass a flag as an arg to your check_status function, and deal with it accordingly.

def check_status(return_dict=False) :
    if return_dict :
        # Return stuff here.
    # Pretty Print stuff here.

And then...

check_status() # Pretty print
not_robot_stat = check_status(True) # Get the dict.

EDIT: I assumed you'd be pretty printing more often than you'd assign. If that's not the case, interchange the default value and the value you pass in.

Stearn answered 2/5, 2009 at 1:16 Comment(0)
G
0

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.

Goar answered 2/5, 2009 at 1:18 Comment(4)
I don't necessarily agree that having a function that returns/prints depending on a value passed to it is bad architecture, but I guess we all have our opinions.Fuqua
engineering and architecture is not about "opinions".Piotr
And other people disagree that the Earth is round, insisting that it's flat instead: that doesn't mean that the Earth's shape is "about opinions", it just confirms that people are quite willing to ignore facts where they conflict with their cherished prejudices.Goar
You're absolutely right, believing that it is okay for a function to return or print output depending on a value passed to it is exactly akin to believing the earth is flat. Yup, that's exactly the same thing. One thing is to have an opinion, but to hold on to it with such stupidity is a whole other monster...Fuqua
S
0

Late answer to this question, but,

This is not exactly the same as what you ask (i.e., is there a way to check if I'm assigning the return value or not). However, it will have a similar effect.

Instead of your function returning a result directly, wrap that result in a class. Then, make the class's __repr__ function show the printout you require.

This way, if called by itself, it will output the class, which triggers the class'es representation to be printed to the interactive terminal.

Conversely, if assigned to a variable, that variable now has been assigned an object of the class. Obviously you'll have to know to retrieve your value of interest from that object.

Example:

class Wrapper:
    def __init__( self, Value ):    self.Value = Value
    def __repr__( self ):    return f"The value is {self.Value}"

def f( Value ):
    return Wrapper(Value)

f(3)
# "The value is 3"

a = f(3)   # or a = f().Value
# no printout

Having said that, in response to some of the comments in this thread, I also disagree that this is not a meaningless question. A good usecase for knowing if the return is being assigned or not is for creating fault-tolerant programs, or in checking postconditions in a contract-based design.

E.g., you might want to ensure that your "void" function isn't being used by someone who is trying to assign stuff from it. This would have been an amazing thing to have in list.sort() e.g. I've lost count of the number of times where I accidentally did a = list.sort() instead of a = sorted( list ), only to have it silently fail with NoneType errors further down the line.

Sapodilla answered 27/3, 2024 at 16:50 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.