<small id='ihG7L'></small><noframes id='ihG7L'>

  • <tfoot id='ihG7L'></tfoot>

          <bdo id='ihG7L'></bdo><ul id='ihG7L'></ul>
      1. <legend id='ihG7L'><style id='ihG7L'><dir id='ihG7L'><q id='ihG7L'></q></dir></style></legend>

      2. <i id='ihG7L'><tr id='ihG7L'><dt id='ihG7L'><q id='ihG7L'><span id='ihG7L'><b id='ihG7L'><form id='ihG7L'><ins id='ihG7L'></ins><ul id='ihG7L'></ul><sub id='ihG7L'></sub></form><legend id='ihG7L'></legend><bdo id='ihG7L'><pre id='ihG7L'><center id='ihG7L'></center></pre></bdo></b><th id='ihG7L'></th></span></q></dt></tr></i><div id='ihG7L'><tfoot id='ihG7L'></tfoot><dl id='ihG7L'><fieldset id='ihG7L'></fieldset></dl></div>

        Python - 如何保存函数

        时间:2023-10-19
            <tbody id='YFwdG'></tbody>
          <tfoot id='YFwdG'></tfoot>
          • <bdo id='YFwdG'></bdo><ul id='YFwdG'></ul>
            <i id='YFwdG'><tr id='YFwdG'><dt id='YFwdG'><q id='YFwdG'><span id='YFwdG'><b id='YFwdG'><form id='YFwdG'><ins id='YFwdG'></ins><ul id='YFwdG'></ul><sub id='YFwdG'></sub></form><legend id='YFwdG'></legend><bdo id='YFwdG'><pre id='YFwdG'><center id='YFwdG'></center></pre></bdo></b><th id='YFwdG'></th></span></q></dt></tr></i><div id='YFwdG'><tfoot id='YFwdG'></tfoot><dl id='YFwdG'><fieldset id='YFwdG'></fieldset></dl></div>

            <small id='YFwdG'></small><noframes id='YFwdG'>

            1. <legend id='YFwdG'><style id='YFwdG'><dir id='YFwdG'><q id='YFwdG'></q></dir></style></legend>

                1. 本文介绍了Python - 如何保存函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  Im 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())
                  

                  推荐答案

                  你要做的是把你的 Python 文件作为一个模块或者一个.

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

                  没有办法让这四个功能自动可用,无论如何,100% 的时间,但你可以做一些非常接近的事情.

                  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.

                  例如,在文件顶部,您导入了 numpy.numpy 是一个已设置好的模块或库,因此只要您 import 它,您就可以在任何时候运行 python 时使用它.

                  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.

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

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

                  例如,如果您将这四个函数复制并粘贴到名为 foobar.py 的文件中,那么您可以简单地执行 from foobar import *.但是,这仅在您在保存代码的同一文件夹中运行 Python 时才有效.

                  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.

                  如果你想让你的模块在系统范围内可用,你必须把它保存在 PYTHONPATH 的某个地方.通常,将其保存到 C:Python27Libsite-packages 即可(假设您运行的是 Windows).

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

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

                  <legend id='26Hw7'><style id='26Hw7'><dir id='26Hw7'><q id='26Hw7'></q></dir></style></legend>

                  <small id='26Hw7'></small><noframes id='26Hw7'>

                          <tbody id='26Hw7'></tbody>
                          <bdo id='26Hw7'></bdo><ul id='26Hw7'></ul>

                          <tfoot id='26Hw7'></tfoot>
                        • <i id='26Hw7'><tr id='26Hw7'><dt id='26Hw7'><q id='26Hw7'><span id='26Hw7'><b id='26Hw7'><form id='26Hw7'><ins id='26Hw7'></ins><ul id='26Hw7'></ul><sub id='26Hw7'></sub></form><legend id='26Hw7'></legend><bdo id='26Hw7'><pre id='26Hw7'><center id='26Hw7'></center></pre></bdo></b><th id='26Hw7'></th></span></q></dt></tr></i><div id='26Hw7'><tfoot id='26Hw7'></tfoot><dl id='26Hw7'><fieldset id='26Hw7'></fieldset></dl></div>