Python - 如何保存功能 [英] Python - How to save functions

查看:202
本文介绍了Python - 如何保存功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Python开始。我有四个功能,工作正常。我想要做的是拯救他们。我想在python中随时调用它们。



下面是我的四个函数的代码:

  import numpy as ui 


def simulate_pheryoor(nsim):
sim = ui.random.choice(3,nsim)
return sims

def simulate_guess(nsim):
guesses = ui.random.choice(3,nsim)
返回猜测

def goat_door(prisedoors,猜测):


结果= ui.random.randint(0,3,prizedoors.size)
而真:
坏=(结果== prizedoors) | (result == guesses)
if bad.any():
返回结果
result [bad] = ui.random.randint(0,3,bad.sum())
$ b $ def switch_guesses(guesses,goatdoors):


result = ui.random.randint(0,3,guesses.size)
True:
bad =(result == guesses)| (result == goatdoors)
如果不是bad.any():
返回结果
结果[bad] = ui.random.randint(0,3,bad.sum())


解决方案

你想要做的是把你的Python文件,并将它用作模块



无法自动使这四个函数可用,无论如何,百分之百的时间,但你可以做的非常接近。



例如,在文件的顶部,您导入了 numpy的 numpy 是一个已经设置好的模块或库,只要您 import 它就可以随时运行python。

>

您想做同样的事情 - 将这4个函数保存到一个文件中,并在需要时将其导入。



< hr>

例如,如果您将这四个函数复制并粘贴到名为 foobar.py 的文件中,那么您可以简单地执行从foobar导入< 。但是,只有在保存代码的同一文件夹中运行Python时,才能使用该功能。



如果您希望在整个系统范围内使用您的模块,必须将其保存在 PYTHONPATH 的某处。通常,将它保存到 C:\Python27\Lib\site-packages 将会工作(假设您正在运行Windows)。


I´m starting in python. I have four functions and are working OK. What I want to do is to save them. I want to call them whenever I want in python.

Here's the code 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())

解决方案

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

There's no way to make those four functions automatically available, no matter what, 100% percent of the time, but you can do something very close.

For example, at the top of your file, you imported numpy. numpy is a module or library which has been set up so it's available any time you run python, as long as you import it.

You want to do the same thing -- save those 4 functions into a file, and import them whenever you want them.


For example, if you copy and paste those four functions into a file named foobar.py, then you can simply do from foobar import *. However, this will only work if you're running Python in the same folder where you saved your code.

If you want to make your module available system-wide, you have to save it somewhere on the PYTHONPATH. Usually, saving it to C:\Python27\Lib\site-packages will work (assuming you're running Windows).

这篇关于Python - 如何保存功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆