English 中文(简体)
在python中重新加载函数?
原标题:Reload a function in python?

嘿,我对python很陌生,但我正在尝试制作一个简单的注册系统。我希望在添加成员时能够进行功能更新,但我不知道如何更新。正如您将在下面的代码中看到的,一旦我在菜单选项2中添加完成员,当我返回并尝试菜单选项1时,我刚刚添加的成员将不会出现。如何使show_members函数重新加载更新后的词典?

除了我上面的问题,我们欢迎您就如何改进以下代码提出任何建议。是的,我知道它是非常不完整的,但它是在制品。

import sys
import shutil
import os

tmp = os.path.isfile("members.py.tmp")
if tmp == True:
    os.remove("members.py.tmp")
shutil.copyfile("members.py", "members.py.tmp")

from members import members

def show_menu():
    os.system("clear")
    print "
","*" * 12, "MENU", "*" * 12
    print "1. List members"
    print "2. Add member"
    print "3. Delete member"
    print "99. Save"
    print "0. Abort"
    print "*" * 28, "
"
    return input("Please make a selection: ")

def show_members(members):
    for keys in members.keys():
        os.system("clear")
        print "
Names", "     ", "Code"
        print keys, " - ", members[keys]

def add_member(members):
    os.system("clear")
    name = raw_input("Please enter name: ")
    code = raw_input("Please enter code: ")
    members[name] = code
    return members


#with open("foo.txt", "a") as f:
#     f.write("new line
")

running = 1

while running:
    selection = show_menu()
    if selection == 1:
        show_members(members)
        print "
> " ,raw_input("Press enter to continue")
    elif selection == 2:
        add_member(members)
        print "
> " ,raw_input("Press enter to continue")
    elif selection == 99:
        shutil.copyfile("members.py.tmp", "members.py")
    elif selection == 0:
        os.remove("members.py.tmp")
        sys.exit("Program Aborted")
    else:
        os.system("clear")
        print "That is not a valid option!"
        print "
> " ,raw_input("Press enter to continue")
最佳回答

在此代码中:

if selection == 2:
    add_member(members)

您将返回成员,但不使用返回值执行任何操作。

最简单的方法是将其更改为:

if selection == 2:
    members = add_member(members)
问题回答

我不会每次都重新加载一个Python文件。相反,只需将您的成员存储在字典中,然后使用pickle库,以便在脚本的开头和结尾加载和保存字典。由于字典是可变的,因此在一个函数中对字典的修改应该显示在其他函数中;您只需在开头和结尾调用pickle.load(file_path泡菜.dump(members,file_path)

要重新加载Python模块,请使用reload()函数(但请注意,只有当源代码文件发生更改时,它才会重新加载!)

还有一件事-您可能想将show_members中的os.system(清除)移到for循环的外部。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签