9

Is it possible to overload the from/import statement in Python?

For example, assuming jvm_object is an instance of class JVM, is it possible to write this code:

class JVM(object):

  def import_func(self, cls):
    return something...

jvm = JVM()

# would invoke JVM.import_func
from jvm import Foo
Barthelemy
  • 8,277
  • 6
  • 33
  • 36
  • @S.Lott Essentially, I want to import packages and classes from a remote JVM (in a Python program) and although I could provide an import method, I would like to reuse Python syntax. – Barthelemy Oct 13 '10 at 21:17
  • "import packages and classes from a remote JVM"? Not a file? How's that going to work? How will you manage the creation of .pyc files? What's wrong with "files"? They work really well. What's wrong with remote mounting a file system? That works really well. – S.Lott Oct 13 '10 at 21:21
  • @S.Lott No need to ask 6 questions in a comment. I asked this question for the Py4J project I'm working on: http://py4j.sourceforge.net/ – Barthelemy Oct 13 '10 at 21:27
  • @Barthelemy: Some people refuse to answer any questions. Some people skim and ignore the questions. Clearly, you're more polite than others. I still don't get why remote mounting a file systems isn't your solution of choice. "py4j" isn't really a very helpful clarification. – S.Lott Oct 13 '10 at 21:34
  • 10
    Why is everyone expecting the question poster to not know what he is talking about? Two answers and a comment to make him wiser. If you all think that an import hook is a silly idea why don't you all take it up with PEP 302 and the Python developers there? – Muhammad Alkarouri Oct 13 '10 at 21:41
  • @S.Lott I did not add too much rationale in the question because I wanted the question to be general enough, but I understand your questions and your concerns, especially now that I see all the warnings in the answers below. Briefly, I just want to call an arbitrary function, I don't want to import a Python module at all. (see next message). – Barthelemy Oct 13 '10 at 21:44
  • Regarding Py4J, this is a project that allows you to interact with a Java Virtual Machine from a Python interpreter (it uses socket under the hood). A bit like Jython, but it runs in a Python interpreter so you can mix Python C extensions and your favorite Java library. Currently, users have to use fully qualified name to instantiate Java classes (e.g., jvm.java.util.ArrayList). I added an import feature, but I wanted to allow users to use this feature like regular imports (again, a bit like Jython). Hope this is clearer :-) – Barthelemy Oct 13 '10 at 21:45
  • "Currently, users have to use fully qualified name to instantiate Java classes (e.g., jvm.java.util.ArrayList)" What's wrong with that? – S.Lott Oct 13 '10 at 21:56
  • @Muhammad Alkarouri: "Why is everyone"? Who's this universe of "everyone"? I don't understand the question. Is that a problem? Should I stop trying to understand the question? – S.Lott Oct 13 '10 at 21:58
  • In an interactive session, package imports are life savers (e.g., I don't like to type java.util or org.eclipse.osgi.framework.util repeatedly!). Maybe it's just me though :-) – Barthelemy Oct 13 '10 at 22:02
  • @S.Lott: Everyone was meant to include the two answers below commenting "rather than as suggested usage" and "do we really need to abuse everything" in addition to your comments. So it is not only you. – Muhammad Alkarouri Oct 13 '10 at 22:05
  • 1
    @Muhammad Alkarouri Maybe I wasn't clear enough. I meant "suggested usage" in terms of actually importing modules from arbitrary remote websites which is a warning that the author of the code I linked to gives as well. However, it is a good example of how to customize the import process (which has all sorts of 'serious' uses), so I linked to it. I in no way meant to condescend OP. – aaronasterling Oct 13 '10 at 22:22
  • Hey, thanks to all for the answers. I must say that like Muhammad Alkarouri, I was surprised by all the warnings and the questioning, but I appreciate all your answers (and your clarification Aaron!). I must run now, but will get back to you ASAP. – Barthelemy Oct 13 '10 at 23:11
  • "In an interactive session"? I'm lost. You're not writing scripts? If your just working interactively, why not use emacs and write a macro? – S.Lott Oct 13 '10 at 23:17
  • @aaronasterling: ah you mean the security implications. I agree with you there. Sorry for the misunderstanding. – Muhammad Alkarouri Oct 14 '10 at 10:02

2 Answers2

7

This post demonstrates how to use functionality introduced in PEP-302 to import modules over the web. I post it as an example of how to customize the import statement rather than as suggested usage ;)

aaronasterling
  • 68,820
  • 20
  • 127
  • 125
4

It's hard to find something which isn't possible in a dynamic language like Python, but do we really need to abuse everything? Anyway, here it is:

from types import ModuleType
import sys

class JVM(ModuleType):
    Foo = 3

sys.modules['JVM'] = JVM

from JVM import Foo
print Foo

But one pattern I've seen in several libraries/projects is some kind of a _make_module() function, which creates a ModuleType dynamically and initializes everything in it. After that, the current Module is replaced by the new module (using the assignment to sys.modules) and the _make_module() function gets deleted. The advantage of that, is that you can loop over the module and even add objects to the module inside that loop, which is quite useful sometimes (but use it with caution!).

tux21b
  • 90,183
  • 16
  • 117
  • 101