Precautions when pickling a function in python

What i did

I tried to pickle a python function and restore it

Conclusion

--The function of the module you are importing can be called correctly from another module. --Functions in the same module are called functions with the same name

Steps taken

  1. Write func () with the same name in test1.py and test2.py
  2. Save func () and test2.func () from test1.py with pickle
  3. Read and execute the pickle file from test1.py and test2.py

result

test1.py


This is test1.func()
This is test2.func()

test2.py


This is test2.func()
This is test2.func()

As mentioned above, test2.py will read func () with the same name. If func () is not in test2.py, an error will occur with "Name Error: name'func' is not defined".

If you import test1.py with test1.py and save test1.func as pickle, it will be read correctly.

code

I put the following two files in the same directory and ran each.

test1.py


import pickle
import test2


def save_pickle(obj, path):
    with open(path, mode='wb') as f:
        pickle.dump(obj, f)


def load_pickle(path):
    with open(path, mode='rb') as f:
        obj = pickle.load(f)
    return obj


def func():
    print('This is test1.func()')


def main():
    func()
    test2.func()

    path = 'test1_func.pickle'
    save_pickle(func, path)
    func_from_pickle = load_pickle(path)
    func_from_pickle()

    path = 'test2_func.pickle'
    save_pickle(test2.func, path)
    func_from_pickle = load_pickle(path)
    func_from_pickle()


if __name__ == '__main__':
    main()

test2.py


import pickle


def load_pickle(path):
    with open(path, mode='rb') as f:
        obj = pickle.load(f)
    return obj


def func():
    print('This is test2.func()')


def main():
    path = 'test1_func.pickle'
    func_from_pickle = load_pickle(path)
    func_from_pickle()

    path = 'test2_func.pickle'
    func_from_pickle = load_pickle(path)
    func_from_pickle()


if __name__ == '__main__':
    main()

Recommended Posts

Precautions when pickling a function in python
Create a function in Python
Precautions when using pit in Python
Precautions when creating a Python generator
When writing a program in Python
[Python] Execution time when a function is entered in a dictionary value
Precautions when giving default values to arguments in Python function definitions
Draw a graph of a quadratic function in Python
To execute a Python enumerate function in JavaScript
Get the caller of a function in Python
Precautions when dealing with control structures in Python 2.6
Create a dictionary in Python
Use callback function in Python
ntile (decile) function in python
A memorandum when writing experimental code ~ Logging in python
Precautions when dealing with ROS MultiArray types in Python
Things to note when initializing a list in Python
What's in that variable (when running a Python script)
Make a bookmarklet in Python
Attention when os.mkdir in Python
Nonlinear function modeling in Python
Draw implicit function in python
Use communicate () when receiving output in a Python subprocess
Immediate function in python (lie)
Draw a heart in Python
What does the last () in a function mean in Python?
A function that divides iterable into N pieces in Python
A memo when creating a directed graph using Graphviz in Python
To return char * in a callback function using ctypes in Python
[python] Manage functions in a dictionary (command table, function table, function pointer)
A memo of writing a basic function in Python using recursion
Precautions that must be understood when building a PYTHON environment
Try running a function written in Python using Fn Project
[Python] What is a zip function?
Maybe in a python (original title: Maybe in Python)
Call a Python function from p5.js.
Implement R's power.prop.test function in python
[python] Manage functions in a list
Function argument type definition in python
Included notation in Python function arguments
Create a DI Container in Python
Behavior when listing in Python heapq
Draw a scatterplot matrix in python
Write AWS Lambda function in Python
ABC166 in Python A ~ C problem
Write A * (A-star) algorithm in Python
Measure function execution time in Python
Create a binary file in Python
Precautions when using the urllib.parse.quote function
[Python] Make the function a lambda function
Solve ABC036 A ~ C in Python
Write a pie chart in Python
Write a vim plugin in Python
Write a depth-first search in Python
Function synthesis and application in Python
Implementing a simple algorithm in Python 2
Create a Kubernetes Operator in Python
Solve ABC037 A ~ C in Python
Run a simple algorithm in Python
Draw a CNN diagram in Python
When creating a matrix in a list