def execute_only_once ( ) : f = inspect . currentframe ( ) . f_back ident = ( f . f_code . co_filename , f . f_lineno ) if ident in _EXECUTE_HISTORY : return False _EXECUTE_HISTORY . add ( ident ) return True
how to perform if function once python
Each called in the code to this function is guaranteed to return True the first time and False afterwards .
def execute_only_once():
    """
    Each called in the code to this function is guaranteed to return True the
    first time and False afterwards.

    Returns:
        bool: whether this is the first time this function gets called from this line of code.

    Example:
        .. code-block:: python

            if execute_only_once():
                # do something only once
    """
    f = inspect.currentframe().f_back
    ident = (f.f_code.co_filename, f.f_lineno)
    if ident in _EXECUTE_HISTORY:
        return False
    _EXECUTE_HISTORY.add(ident)
    return True