How to find where a Python class is defined
python get namespace
python get module of class
python inspect example
python find all classes in directory
python show class
python get class of method
python code object
Let's say I have a number in Python project and I want to find out in what package/module a particular class is defined (I know the class name). Is there an efficient way to do it?
You can use the
inspect module to get the location where a module/package is defined.
<module 'module_name' from '/path/to/my/module.py'>
As per the docs,
inspect.getmodule(object) Try to guess which module an object was defined in.
28.13. inspect — Inspect live objects — Python 2.7.18 documentation, They also help you determine when you can expect to find the following special attributes: name of module in which this class was defined. A class creates a new local namespace where all its attributes are defined. Attributes may be data or functions. There are also special attributes in it that begins with double underscores __. For example, __doc__ gives us the docstring of that class. As soon as we define a class, a new class object is created with the same name.
Let's explain by example
import numpy print numpy.__file__
on my machine.
If you only have a class, you can use that class with the python2-ic
#let unknownclass be looked for import imp modulename = unknownclass.__module__ tup = imp.find_module(modulename) #(None, '/usr/lib64/python2.7/site-packages/numpy', ('', '', 5)) print "path to module", modulename, ":", tup #path to module numpy : /usr/lib64/python2.7/site-packages/numpy
As you can see, the
__module__ property is probably what you're looking for.
9. Classes — Python 3.8.5 documentation, Class instances can also have methods (defined by its class) for modifying its normally class members (including the data members) are public (except see� The find() method finds the first occurrence of the specified value. The find() method returns -1 if the value is not found. The find() method is almost the same as the index() method, the only difference is that the index() method raises an exception if the value is not found. (See example below)
One way to get the location of a class is using a combination of the
__module__ attribute of a class and the
import sys from stl import stl module = sys.modules[stl.BaseStl.__module__] print module.__file__
I should note that this doesn't always give the correct module, it just gives the module where you got it from. A more thorough (but slower) method is using the
from stl import stl import inspect print inspect.getmodule(stl.BaseStl).__file__
inspect — Inspect live objects — Python 3.8.5 documentation, They also help you determine when you can expect to find the following getmembers() will only return class attributes defined in the metaclass when the� A class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made.
3. Data model — Python 2.7.18 documentation, See the documentation of the gc module for information on controlling the collection A user-defined method object combines a class, a class instance (or None )� Despite importing the .py file containing my class, python is insistent that the class doesn't actually exist. class def: class greeter: def __init__(self, arg1=None): self.text = arg1 def sayHi(self): return self.text
How do I check if an object is an instance of a given class or of a , and can also check whether an object is one of Python's built-in types. Note that most programs do not use isinstance on user-defined classes very often. Reading self.currentid in the on_close method will fail to find a currentid attribute in the object self, so Python will look at the class of self which is SocketHandler. That object does have a currentid value, so the result of reading self.currentid will be 0, whether or not you've previously run open on that SocketHandler.
Objects and Classes in Python, A Circle class defines an attribute named radius and three methods namely Python uses self parameter to know which object to operate on� An inner class or nested class is a defined entirely within the body of another class. If an object is created using a class, the object inside the root class can be used. A class can have more than one inner classes, but in general inner classes are avoided. Related Course: Python Programming Bootcamp: Go from zero to hero. Inner class example
- Are you asking programatically or just in general?
- Related: stackoverflow.com/a/31166641/3001761
- @stazima Any way would work for me.
- do you know what the guessing part means? when will it get it wrong?
inspect.getmodule(my_class).__file__to get the guessed file name.
- That gives the location of a module/package, not a class.
- That's how python works; modules have files, which contain classes. Of course, there might be automatic class finding based on file names, but it's generally not like a class must come from a file, so this is not really something that you can expect of python -- finding the module where the class is from is in many cases the closest you get.
- That is true, but that makes it the answer to a different question :) Not downvoting, just pointing it out.
- @Wolph: do you like my last paragraph?
- Much better, great change :)