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 606853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:17:53+00:00 2026-05-13T17:17:53+00:00

In python 2.5, I have the following code in a module called modtest.py: def

  • 0

In python 2.5, I have the following code in a module called modtest.py:

def print_method_module(method):
    def printer(self):
        print self.__module__
        return method(self)
    return printer

class ModTest():

    @print_method_module
    def testmethod(self):
        pass

if __name__ == "__main__":
    ModTest().testmethod()

However, when I run this, it prints out:

__main__

If I create a second file called modtest2.py and run it:

import modtest

if __name__ == "__main__":
    modtest.ModTest().testmethod()

This prints out:

modtest

How can I change the decorator to always print out modtest, the name of the module in which the class is defined?

  • 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-05-13T17:17:54+00:00Added an answer on May 13, 2026 at 5:17 pm

    When you execute a python source file directly, the module name of that file is __main__, even if it is known by another name when you execute some other file and import it.

    You probably want to do like you did in modtest2, and import the module containing the class definition instead of executing that file directly. However, you can get the filename of the main module like so, for your diagnostic purposes:

    def print_method_module(method):
        def printer(self):
            name = self.__module__
            if name == '__main__':
                filename = sys.modules[self.__module__].__file__
                name = os.path.splitext(os.path.basename(filename))[0]
            print name
            return method(self)
        return printer
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have the following code in a module called code_database.py class Entry(): def enter_data(self):
consider I have the following code in python module a.py : def func(): obj
In python I have the following function: def is_a_nice_element(element, parameter): #do something return True
A simple question on a python module. Let's say I have the following code:
I have the following code (as an example) in a unittest TestCase def test(self):
I have the following Python code: def find_words(letters): results = set() def extend_prefix(w, letters):
I have the following code in python from selenium import webdriver from selenium.webdriver.common.by import
So I have the following code GAE code in python and it is a
I'm working in Python. I have the following code: while not is_suffix(pattern[:k], pattern[:q]): k
I'm just learning about python. I'm fairly new. I have the following code that

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.