3

I have a file structured like this:

imports

def myFunc(spam):
    etc

class MyClass():
    def someMethod(self):
         myFunc(eggs)

I don't think this works as I understand that functions assume scope local to just that function. How would I accomplish this? It seems silly to import itself and then call imported.myFunc()

In case there are those that need to know why -- this is a file called utilities and the class is Database that contains my database wrapper stuff. Outside of it are utility functions. I'd prefer to keep Database inside of utilities, if possible.

2
  • 1
    What happened when you tried? Hint: it works fine. Commented Mar 25, 2015 at 2:17
  • I guess I could have put some effort into trying it out -- the immediate code was actually an exception so it wasn't as simple as just running the code. Commented Mar 25, 2015 at 3:20

2 Answers 2

2

The scope of a given identifier is determined by the location of its definition. myFunc is defined at the file level, so it is visible within someMethod since it is in the same file.

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

Comments

0

You should be able to call any function outside your Class with the normal function call (as you have it in someMethod).

If you prefer to have the function inside your Class as a method, the call would then be self.myFunct(...).

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.