0

I have three python files

one.py, two.py, three.py

one.py

from one.py I call

import two as two  
    two.three()

in two.py I have

def two():
    "catch the error here then import and call three()"
    import three as three

in three.py I have

def three():
    print "three called"

so naturally I am getting:

AttributeError: 'function' object has no attribute 'three'

My question is:

Is there a way to have two.py capture the error then import three.py and then call three()?

______________edit________________V
I can call it like this:

two().three()

def two():
    import three as three
    return three

but I would like to call it like so:

two.three()

So essentially it would auto exec def two():

7
  • 3
    Could you explain what you're trying to achieve (on a broader level)? Commented Jan 24, 2013 at 23:36
  • Would I be correct in assuming that you mean to say from two import two and from three import three instead? Commented Jan 24, 2013 at 23:40
  • I am trying to create a global object that I can call. so the import will happen then the functions will be available after the import. Commented Jan 24, 2013 at 23:40
  • 2
    Why not just import them right away? You probably can add new functions to a module after it's been created but this seems more confusing than useful. (Posing a question this abstract is a good way to cause the XY problem.) Commented Jan 24, 2013 at 23:47
  • The module two does not contain three when you import it; so you should get an error. But your error suggests that one sees two as a function, not as a module. This is impossible with the code you give: you probably have from two import two, as @neir suggests? Commented Jan 24, 2013 at 23:50

2 Answers 2

1

Here's the solution I came up with. I confess that I was inspired by your question to try to figure this out, so I don't completely understand it myself. The magic happens in two.py where the attempt to access and then call the three method of two is handled by the __getattr__ method of the method_router class. It uses __import__ to import the indicated module by name (a string), and then imitates the from blah import blah by calling getattr() again on the imported module.

one.py

from two import two
two.three()

two.py

class method_router(object):
    def __getattr__(self, name):
        mod = __import__(name)
        return getattr(mod, name)

two = method_router()

three.py

def three():
    print("three called")
Sign up to request clarification or add additional context in comments.

1 Comment

I wanted to do it like this: two.three() but wont work it will work like this two().three() def two(): import three as three return three
0

When you are invoking a module there is no way the invoked module can check by itself whether it has a function and if not follow an alternative path. You could wrap two.three() around try except clause catching an attribute error.

try:
   two.three()
except AttributeError:
   three.three()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.