We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,329 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

When __name__ not equal to module name

#module nametest

def showName():
   print("__name__ is: " + str(__name__))

If I import nametest into another module or into the shell interpreter and call nametest.showName() I find that name = "nametest", in other words name gets the module name it is a built-in member of?

The only exception I know of is if I define showName directly in the shell without importing it in a module, then name shows "main". Is there any other way to get the function "showName()" to print a value other than "nametest".

2
Contributors
4
Replies
1 Day
Discussion Span
6 Months Ago
Last Updated
5
Views
Question
Answered
drichird
Light Poster
26 posts since Sep 2007
Reputation Points: 11
Solved Threads: 0
Skill Endorsements: 0

Here is one

import nametest
nametest.__name__ = "foobar"
nametest.showName()
Gribouillis
Posting Maven
Moderator
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11

Thanks Gribouillis So (sorry if this is obvious question) would it be correct to say the main purpose of name is to verify what module the particular function/class was defined in? (As opposed to where it is called from). Other than passing self or something similar as an argument to a function, is there any dynamic builtin variable a function can use to print out where it was called from, not knowing ahead of time how it might be used. (Other than toggling the debugger).

By the way, does nametest.name = .... reset the original variable or create a new one?

drichird
Light Poster
26 posts since Sep 2007
Reputation Points: 11
Solved Threads: 0
Skill Endorsements: 0

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
Moderator
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11

Excellent! Thank you Gribouillis...

drichird
Light Poster
26 posts since Sep 2007
Reputation Points: 11
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 6 Months Ago by Gribouillis

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0673 seconds using 2.66MB