Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get self.__module__ of class regardless of how imported

Tags:

python

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
like image 706
dmyung Avatar asked Nov 04 '12 02:11

dmyung


People also ask

What does self __ class __ do?

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.

What is import* in Python?

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.

What happens when a module is imported in Python?

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.

What is __ module __?

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.

What is self in Python class?

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.

Why do we use self in classes in Java?

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.

Is self an instance of the current class?

Note that self may not be an instance of the current class, so the following is not quite foolproof.

What does the built-in class attribute of __module__ do?

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


1 Answers

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?

like image 88
Cory Avatar answered Sep 19 '22 14:09

Cory