1

I have 2 py files in the same folder (IDE Pycharm):

module1.py contains:

def test_func():
    return "test_func"

class test_class(object):
    def __init__(self):
        self._test = test_func()

driver.py contains:

from module1 import test_class
obj = test_class()
print obj._test

I was expecting the the driver.py to fail with as the class instantiation requires the test_func() to be in scope.

To my surprise it didn't fail. I am not sure why (in fact there are a few posts in Stack Overflow asking about how to make function globally available to be used inside of class).

Is there such thing as "closure" on function object?

What is the difference between my 'imaginary problem' and this SO question? create a global function in python

EDIT

As explained below, the SO question was to do with calling sys.exit. It has nothing to do with the function scope per say.

3
  • When self._test = test_func() is run, Python looks first in the function namespace for the name test_func, and failing to find it, looks to the next level -- the module namespace. There isn't really a closure involved, I don't think. Note also that the name is only resolved when __init__ runs... not when it is defined. Commented Nov 30, 2015 at 2:57
  • there is no __init__.py in the same folder. the driver.py specifically import only the class definition not the entire content of the file. How could python actually find the this function? otherwise there is never a need to import anything as long as they are in the same folder? Commented Nov 30, 2015 at 3:01
  • This isn't a closure (they are functions defined in functions). A class's global namespace is the module where the class is defined, not the module where the class happens to be used. When driver.py instantiates test_class, module1.py is used to lookup names. It would be bizarre any other way. Imagine if you always needed to implement all supporting functions just to instantiate someone else's class. Commented Nov 30, 2015 at 3:03

1 Answer 1

2

test_func is "in scope" within the class implementation. It is not visible from driver.py since it is not imported. Python using lexical scoping so the scope is determined at compile time. Everything within module1.py is available to the implementation of test_class regardless of where it is called from. The Wikipedia article on scoping explains the difference between lexical and dynamic scoping.

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

4 Comments

Ok, i am ok with the class scope explaination. but i thought i would have the same problem as this SO question: stackoverflow.com/questions/5761408/… Maybe i need some sleep.
The problem in the linked question was due to a call to sys.exit within notify exiting the program.
Thanks guys for all the answers and explanations. Some sleep is needed for sure.
I do think that SO question should not mark that reply as the answer. or at least it need a better explanation in the answer

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.