Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I Get the Module Name of an Object's Class Definition Rather Than the Module Name of the Object's Instantiation?

Tags:

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?

like image 320
Brent Newey Avatar asked Feb 17 '10 16:02

Brent Newey


People also ask

How can we find the names that are defined inside the current module?

Python dir function The dir function returns all the names available in the current namespace of the module.

Can a module and class have the same name in Python?

There are times when a module and a class in it have the same name. Then, in order to access the class name using method 1 (see p. 1), you must prefix it with the module name. If you do not specify a module name, an exception will be thrown.

What is __ repr __ in Python?

Python __repr__() function returns the object representation in string format. This method is called when repr() function is invoked on the object. If possible, the string returned should be a valid Python expression that can be used to reconstruct the object again.

What is __ module __ in Python?

A module in Python is a file (ending in .py ) that contains a set of definitions (variables and functions) that you can use when they are imported. Modules are considered as objects, just as everything else is in Python. Many methods can operate on modules.

What is __ class __ in Python?

__class__ is an attribute on the object that refers to the class from which the object was created. a. __class__ # Output: <class 'int'> b. __class__ # Output: <class 'float'> After simple data types, let's now understand the type function and __class__ attribute with the help of a user-defined class, Human .

What is the __ str __ method used for?

The __str__ method should be defined in a way that is easy to read and outputs all the members of the class. This method is also used as a debugging tool when the members of a class need to be checked. The __str__ method is called when the following functions are invoked on the object and return a string: print()


1 Answers

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 
like image 130
Matt Anderson Avatar answered Oct 11 '22 02:10

Matt Anderson