# You can get any function's orclass's name as a string with __name__.
# In fact, using just __name__ will return a module's name, or"__main__"# if the file is the current running scriptdefthis_is_my_name():
pass
class MyName:
def__init__(self) -> None:
pass
def__str__(self) -> str:
# this is just so we can get a more readable outputreturn"MyName Object"# as you can see later on, you can't get a variable's name the same way
named_variable =231
named_instance =MyName()
list_of_stuff_w_names = [this_is_my_name, MyName, named_instance, named_variable]
print ("- Script running in", __name__)
for stuff_w_name in list_of_stuff_w_names:
try:
print ("-", f"{stuff_w_name.__name__ = }")
except AttributeError:
print ("-", stuff_w_name, "doesn't have a __name__!")
"""
OUTPUT:
- Script running in __main__
- stuff_w_name.__name__ ='this_is_my_name'- stuff_w_name.__name__ ='MyName'- MyName Object doesn't have a __name__!-231 doesn't have a __name__!"""
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems
resetting your password contact us
Check Your Email and Click on the link sent to your email