I have a library class where depending how it's imported, a method that relies on the self.__module__
for identification changes behavior - depending on if I import it relatively or absolutely. Is there a way to force the self.__name__
attribute of a class to return itself absolutely?
I realize one solution would be to force everyone to import the subclasses in the same way, but was wondering if there was a way to force it from the library's standpoint.
Summarized Structure
I have a module in a library
project/
mylib/
foo.py
LibraryClass
def get_name(self):
return "%s.%s.%s" % \
(self.__module__, self.__class__.__name__, self.some_init_property)
prog/
utils.py
fooClass(LibraryClass)
bar.py
def some_func()
#see below
In mylib
, I have an importer import all subclasses of LibraryClass
via a the string of the classname in our django project's settings file.
Basically, the behavior we're seeing is depending on how you import. So there are two behaviors I'm observing in bar.py:
def some_func_absolute():
from prog.utils import fooClass
f = fooClass("lalala")
print f.get_name()
#prints prog.utils.fooClass.lalala
Versus
def some_func_relative():
from utils import fooClass
f = fooClass("lalala")
print f.get_name()
#prints foo.fooClass.lalala
self. __class__ is a reference to the type of the current instance. Throwing an exception here is like using an assert statement elsewhere in your code, it protects you from making silly mistakes. type() should be preferred over self.
In Python, you use the import keyword to make code in one module available in another. Imports in Python are important for structuring your code effectively. Using imports properly will make you more productive, allowing you to reuse code while keeping your projects maintainable.
When a module is first imported, Python searches for the module and if found, it creates a module object 1, initializing it. If the named module cannot be found, a ModuleNotFoundError is raised. Python implements various strategies to search for the named module when the import machinery is invoked.
The __module__ property is intended for retrieving the module where the function was defined, either to read the source code or sometimes to re-import it in a script.
self in Python class. self represents the instance of the class. By using the “self” keyword we can access the attributes and methods of the class in python. It binds the attributes with the given arguments.
We'll use self in classes to represent the instance of an object. We can create multiple of a class and each instance will have different values. And self helps us to get those property values within the class instance.
Note that self may not be an instance of the current class, so the following is not quite foolproof.
This built-in class attribute when called prints the name of the module the function/object was defined in, or None if unavailable. The following code shows how __module__ works
I suspect that the right thing here is to never have parent/child directories in the python path. I also suspect that this might not be possible given that you do. Depending what you're doing, maybe you could use .__file__
instead which should still be consistent no matter how it's imported?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With