Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python -- calling a function in same file as a class from within an instance of that class?

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.

like image 215
linus72982 Avatar asked Jan 02 '26 04:01

linus72982


1 Answers

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.

like image 89
mike d Avatar answered Jan 03 '26 17:01

mike d



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!