简体   繁体   中英

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.

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 import ed itself.

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

Firstly its advisable to use from somefile import definition as something let me use your function definitions to give you a clear picture. I will write simple functions to eat and chew. where chew will references eat.

useful.py

def eat():
    print 'Eaten'

def chew():
    eat()

main File.py code

from useful import chew as uf

uf()

By calling uf alias for chew function which references the eat function. Welcome to python.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM