[Python] Is there a way to tell if a function's return value is used?

Discussão em 'Python' iniciado por Stack, Julho 11, 2025.

  1. Stack

    Stack Membro Participativo

    Is there a way to determine if the return value of a function is needed, from inside that function?

    Example:

    drawPoint(1,2)


    vs.

    newPointId=drawPoint(1,2)


    drawPoint makes an http request, so, the response from that request might be received from the server 'right away' or 'soon' or 'after a while' or never.

    If the return value is not needed (the first case), the command could be 'fire-and-forget': just an immediate return with irrelevant value after the request is sent with zero timeout.

    If the return value is needed (the second case), then the command should be 'blocking', at least for some timeout period. Various retry schemes and timeout values could be employed. (If no response is received in the desired timeframe, it could return False, and the rest of the code would need to be able to deal with that case; but there's an expectation that the variable 'newPointId' should be usable on the next line of code.)

    def drawPoint(x,y):
    if [returnValueNeeded]:
    # do a blocking request, with retry scheme etc.
    # return its response or whatever part of its response is needed
    else:
    # do a non-blocking 'fire-and-forget' request with zero timeout
    # return with any value or no value

    Continue reading...

Compartilhe esta Página