Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python modules: functions calling each other

I have got myself very confused. I have a set of python functions, all of which I've bunged together in a file called (let's say) useful.py. I can then read the module into my ipython with

 import useful as uf

and then I can access individual functions with

 uf.meaning_of_life()

and so on. Standard stuff.

However - some functions in this file call on other functions. I can call a single function any time with uf., but what about functions which call each other? If a function called eat makes reference to another function called chew, how does eat know where to find chew? I can call both as uf.eat and uf.chew.

I can ignore all of this by simply doing execfile('useful.py') which works perfectly well, but I would like to get more of a handle on the module system.

Currently when I use import my attempt to use my functions produces errors; when I use execfile everything works fine.

I appreciate that this might be construed as very much a beginners question, but I am coming to Python from a Matlab background, and my natural inclination is to use execfile. Pointers to information would be very welcome.

like image 240
Alasdair Avatar asked Jan 10 '15 11:01

Alasdair


People also ask

Can Python functions call other Python functions?

In Python, any written function can be called by another function.

Can 2 functions call each other?

Yes two functions can call each other.

How do you call two functions in Python?

The most common way is to use the threading module. Threading allows you to create “threads” that will execute independently of each other. Another way to run multiple functions simultaneously is to use the multiprocessing module, which allows you to create “processes” that will execute independently of each other.


1 Answers

What you need to understand first in order to get what's happening is that every module is its own local namespace.

If you have a module like this:

def chew():
    print('chewing')

def eat():
    print('eating')
    chew()

Isn't it obvious that eat can call chew without any problems? They're defined at the same level, in the global (module) scope, so clearly they can see each other.

Now we do the following in another module:

import useful as uf

uf.eat()

In order to access chew, we need to write uf.chew, the same way we have typed uf.eat. We need to do this because we are in another module. In order for us to do any of that we have had to import useful, so that our module knows of it. Our knowledge of the useful module is that its contents are added to the uf name.

That is our point of view. But the useful module knows everything about itself. Nothing has changed there, it doesn't need to import itself to access its contents, and it doesn't know anything about other modules that it has not imported itself.

So to answer, eat knows how to find chew because they are in the same module, and therefore in the same scope.

like image 60
Darkhogg Avatar answered Oct 22 '22 18:10

Darkhogg