Last active
June 27, 2022 06:42
-
-
Save GadgetSteve/5dfef779ca679bc8301b2c83047666b7 to your computer and use it in GitHub Desktop.
Importable execution position in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Importable get called from file & location. | |
Based on a tweet by Ned Batchelder @nedbat | |
""" | |
import inspect | |
def where_am_i(): | |
""" Return the current filename and line number. """ | |
bf = inspect.currentframe().f_back | |
return (bf.f_globals.get("__file__", "interactive"), bf.f_lineno) | |
if __name__ == "__main__": | |
print(f"At {where_am_i()[1]} of {where_am_i()[0]}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This would typically be used something like: