When after loading I save through a function, the above error occurs.

1, 2, 3, 4 

But when I save without a function, everything works.

 1, 2, 3, 2 

Why is that?

 import json import os filename = fr"{os.environ['userprofile']}\desktop\file.json" class Class(): def __init__(self): self.age = 100 def save(filename, age): with open(filename,'w') as f_object: a = {} a["users"] = {"Jack": {"age": age}} json.dump(a, f_object, indent=4) while True: var = input("1-new, 2-save, 3-load, 4-save_def, 5-quit: ") if var == "1": instance = Class() elif var == "2": with open(filename,'w') as f_object: a = {} a["users"] = {"Jack": {"age": instance.age,}} json.dump(a, f_object, indent=4) elif var == "3": with open(filename) as f_obj: save = json.load(f_obj) age = save["users"]["Jack"]["age"] instance = Class() instance.age = age elif var == "4": save(filename, instance.age) elif var == "5": quit() 

    2 answers 2

    In the line save = json.load(f_obj) you replace the save function with a dictionary.

      Need to rename or function

       def save(filename, age): 

      or the save variable in save = json.load(f_obj) .