0

I am having two python files as 1.py and 2.py.

**1.py is as** 
class A:
       def __init__(self):
              x = 5
              y = 7
              NUMBERS = self
              fp = open(filePath)
              temp = fp.read()
              exec(temp)
              fp.close()
              ADD_METHOD()

**2.py is as**
    def ADD_METHOD():
          print NUMBERS.x + NUMBERS.y

Now My question how this NUMBERS varaible is available in 2.py file. This is example for better view of problem, i know i can do this by importing module but the problem is that i have to do solution with exec() method and ho can i get NUMBERS in 2.py file ie should i have to pass this with exec as argument or some other approach.

Any Help really appreciable Thanks

4
  • Are you talking about the exec statement, or about os.exec()? Commented Jul 16, 2010 at 10:02
  • i hope this "exec(2.py)" should be statement Commented Jul 16, 2010 at 10:27
  • exec doesn't take filenames, so that doesn't help. Commented Jul 16, 2010 at 11:07
  • fp = open(2.py) temp = fp.read() fp.close() exec(temp) can we do now this Commented Jul 16, 2010 at 11:43

2 Answers 2

2

If these are both Python files that you wrote yourself, why not just create a function in 2.py that you can import and call in 1.py? This is a much easier and cleaner abstraction. It also avoids the creation of a new process. You could write something like this:

# **1.py is as**
from 2 import ADD_METHOD
class A:
       def __init__(self):
              x = 5
              y = 7
              ADD_METHOD(x, y)

# **2.py is as**
    def ADD_METHOD(x, y):
          print x + y

Note that you really should pick better names than 1.py and 2.py to avoid confusion later ;)

Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure what exactly you need, but I have a solution to your trivial example that follows your limitations.

# 1.py
...
os.execl( "2.py", str( NUMBERS.x ), str( NUMBERS.y ) )

# 2.py
from collections import namedtuple
A = namedtuple( "A", "x y" )
NUMBERS = A( x=sys.argv[1], y=sys.argv[2] )
...

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.