Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9213171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:41:03+00:00 2026-06-18T01:41:03+00:00

def a(): print __name__ Is there any chance I can have such a function,

  • 0
def a():
    print __name__

Is there any chance I can have such a function, but have it print the “correct” module name if imported by another module and called from there?

“Correct” module name should always be the one it is called from.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:41:04+00:00Added an answer on June 18, 2026 at 1:41 am

    The sys module provides a CPython specific way to lookup your caller:

    sys._getframe(1).f_globals.get('__name__', '__main__')
    

    The _getframe() function is documented here, and frame objects are documented here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It's my script: class shape: def __init__(self, name): self.name = name def printMyself(self): print
Suppose I have the following object: class Foo(object): def __init__(self, name=None): self.name = name
import subprocess def volumeCheck(volume_name): This function will check volume name is mounted or not.
I have a python question, but i think any programer would know enough to
Is there any difference at all between these classes besides the name? class WithClass
Suppose myapp/foo.py contains: def info(msg): caller_name = ???? print '[%s] %s' % (caller_name, msg)
This Python: data = {} def f(): pass import sys help(sys.modules[__name__]) Prints this: Help
I'm using this simple function: def print_players(players): tot = 1 for p in players:
After defining the following : abstract class A { type T def print(p: T)
I have a method which calls a method on an integer: def print_time(time =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.