Python - How to save functions

2019-03-15 18:20发布

问题:

I´m starting in python. I have four functions and are working OK. What I want to do is to save them. I want to call them whenever I want in python.

Here's the code my four functions:

import numpy as ui


def simulate_prizedoor(nsim):
    sim=ui.random.choice(3,nsim)
    return sims

def simulate_guess(nsim):
        guesses=ui.random.choice(3,nsim)
        return guesses

def goat_door(prizedoors, guesses):


        result = ui.random.randint(0, 3, prizedoors.size)
        while True:
            bad = (result == prizedoors) | (result == guesses)
            if not bad.any():
                return result
            result[bad] = ui.random.randint(0, 3, bad.sum())

def switch_guesses(guesses, goatdoors):


            result = ui.random.randint(0, 3, guesses.size)
            while True:
                bad = (result == guesses) | (result == goatdoors)
                if not bad.any():
                    return result
                result[bad] = ui.random.randint(0, 3, bad.sum())

回答1:

What you want to do is to take your Python file, and use it as a module or a library.

There's no way to make those four functions automatically available, no matter what, 100% percent of the time, but you can do something very close.

For example, at the top of your file, you imported numpy. numpy is a module or library which has been set up so it's available any time you run python, as long as you import it.

You want to do the same thing -- save those 4 functions into a file, and import them whenever you want them.


For example, if you copy and paste those four functions into a file named foobar.py, then you can simply do from foobar import *. However, this will only work if you're running Python in the same folder where you saved your code.

If you want to make your module available system-wide, you have to save it somewhere on the PYTHONPATH. Usually, saving it to C:\Python27\Lib\site-packages will work (assuming you're running Windows).



回答2:

If you decide to put them anywhere in your project folder don`t forget to create a blank init.py file so python can see them. A better answer can be provided here : http://docs.python.org/2/tutorial/modules.html



回答3:

Save them in a file - this makes them a module.

If you put them in a file called mymod.py, in python you can load them as follows

from mymod import *
simulate_prizedoor(23)


回答4:

Quick solution, without having to explicitly create a file - relies on IPython and its storemagic

IPython 4.0.1 -- An enhanced Interactive Python.
details.

In [1]: def func(a):
   ...:     print a
   ...:     

In [2]: func = _i #gets the previous input

In [3]: store func #store(magic) the input 
                   #(auto-magic enabled or would need '%store')
Stored 'func' (unicode)

In [4]: exit

IPython 4.0.1 -- An enhanced Interactive Python.

In [1]: store -r func #retrieve stored string

In [2]: exec func #execute string as python code

In [3]: func(10)
10

Once you had stored all your functions just once, then you can restore them all with store -r, and then exec func once for each function, in each new session.

(Came across this question while looking for a solution for 'quick saving' functions (most convenient way) while in an interactive python session - adding my current best solution for future readers)