Here is one
import nametest
nametest.__name__ = "foobar"
nametest.showName()
Gribouillis
Posting Maven
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11
You can tell where the function was called from using module inspect
#!/usr/bin/env python
# -*-coding: utf8-*-
from __future__ import unicode_literals, print_function
__doc__ = """ sandbox.funccall -
show how to print where a function was called from
"""
import inspect
import os
def calling_line(level):
level = max(int(level), 0)
frame = inspect.currentframe().f_back
try:
for i in xrange(level):
frame = frame.f_back
lineno, filename = frame.f_lineno, frame.f_code.co_filename
finally:
del frame
return lineno, filename
def thefunc(*args):
lineno, filename = calling_line(1)
print("thefunc() was called from {0} at line {1}".format(
os.path.basename(filename), lineno))
return 3.14
def foo():
x = thefunc() ** 2
if __name__ == "__main__":
foo()
""" my output -->
thefunc() was called from funccall.py at line 32
"""
Otherwise, yes, nametest.__name__ = ... resets the original variable. __name__ is an ordinary global variable which is automatically inserted in the module's namespace. Another interesting variable which exists only for imported modules is__file__.
Gribouillis
Posting Maven
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11
Question Answered as of 6 Months Ago by
Gribouillis