Python - how to save functions

I start with python. I have four functions and work fine. I want to save them. I want to call them whenever I want in python.

Here is the code for 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()) 
+6
source share
4 answers

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

It is impossible to make these four functions automatically available, no matter what is 100% percent of the time, but you can do something very close.

For example, at the top of the file, you imported numpy . numpy is a module or library that has been configured in such a way that they are available every time python starts if you import .

You want to do the same - save these 4 functions in a file and import them whenever you want.


For example, if you copy and paste these four functions into a file called foobar.py , you can simply do from foobar import * . However, this will only work if you use Python in the same folder where you saved the code.

If you want your module to be available on the system, you need to save it somewhere on PYTHONPATH . Usually saving it to C:\Python27\Lib\site-packages will work (assuming you are using Windows).

+10
source

If you decide to place them in any project folder, remember to create an empty init .py file so that python can see them. You can get a more efficient answer here: http://docs.python.org/2/tutorial/modules.html

+4
source

Save them in a file - this makes them 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) 
+3
source

A quick fix 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 have saved all your functions only once, you can restore them all with store -r and then exec func once for each function in each new session.

(Turned to this question looking for a solution for quick save functions (the most convenient way) in a python interactive session - adding my current best solution for future readers)

+2
source

All Articles