[Tutor] problems pickling functions

Kent Johnson kent37 at tds.net
Fri Dec 8 15:43:19 CET 2006


Arild B. Næss wrote:
> Den 8. des. 2006 kl. 14.05 skrev Kent Johnson:
>> Why do you need to pickle the function? Is it created dynamically?  
>> Can you just pickle the data?
>>
>> Kent
>>
> 
> Thanks.
> 
> I guess it's not absolutely necessary to pickle the function. I tried  
> to do this because I wanted to use the function in the interpreter  
> without having to write it in there line by line.
> 
> I'm used to working in R and Matlab, where you often run scripts from  
> the active interpreter. In that way  you can actually examine the  
> data a script generates, instead of having the script print it to  
> screen or file.
> 
> I'm having trouble getting used to python like this because I get  
> trouble trying to paste in several lines at once from emacs, and I  
> haven't found a way to run scripts in the interpreter.

Two suggestions:
- Use an editor / IDE that allows you to run Python scripts. IDLE will 
do this. I think emacs has good support for Python too but someone who 
uses emacs will have to help you with that one.

- Save your function in a module and import the module from the 
interpreter. Then you can run the function in the interpreter.

For example if you have funcs.py in the working directory and it 
contains a function
def doItAll():
   pass

then in the interpreter you can type
 >>> import funcs
 >>> funcs.doItAll()

to run the function.

If you change the function in an external editor you have to reload it 
in the interpreter to get the revised version:
 >>> reload(funcs)

Kent

PS Please reply to the list, not to me directly.



More information about the Tutor mailing list