32
loading...
This website collects cookies to deliver better user experience
import inspect
func = inspect.currentframe().f_back.f_code
print(f"in function 'my_function' called from NAME: {func.co_name} in FILENAME: {func.co_filename} on LINE#: {func.co_firstlineno}")
def helper_function(value):
return value.isnumeric()
def adder(num1, num2):
return int(num1) + int(num2)
def main():
print("The most useless calculator!")
first_number = input("Please enter a number: ")
second_number = input("Please enter another number: ")
print(f"Let's add {first_number} and {second_number}")
if helper_function(first_number) and helper_function(second_number):
total = adder(first_number, second_number)
print(f"The total is {total}")
else:
print("Sorry, we cannot add these values...")
main()
def helper_function(value):
return value.isnumeric()
def adder(num1, num2):
import inspect
func = inspect.currentframe().f_back.f_code
print(
f" **** in function 'adder' called from NAME: {func.co_name} in FILENAME: {func.co_filename} on LINE#: {func.co_firstlineno}"
)
return int(num1) + int(num2)
def main():
print("The most useless calculator!")
first_number = input("Please enter a number: ")
second_number = input("Please enter another number: ")
print(f"Let's add {first_number} and {second_number}")
if helper_function(first_number) and helper_function(second_number):
total = adder(first_number, second_number)
print(f"The total is {total}")
else:
print("Sorry, we cannot add these values...")
main()
>>> python3 watcher.py
The most useless calculator!
Please enter a number: 4
Please enter another number: 2
Let's add 4 and 2
**** in function 'adder' called from NAME: main in FILENAME: watcher.py on LINE#: 15
The total is 6
def caller_id(my_func):
def wrapper(*args, **kwargs):
import inspect
my_func(*args, **kwargs)
func = inspect.currentframe().f_back.f_code
print(
f"in function {my_func. __name__ } called from NAME: {func.co_name} in FILENAME: {func.co_filename} on LINE#: {func.co_firstlineno}"
)
return wrapper
@caller_id
def adder(num1, num2):
return int(num1) + int(num2)
If you found this helpful, let me know on Twitter!
The post A caller id for your python function _was originally published at _flaviabastos.ca