Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从脚本中的特定步骤开始?_Python - Fatal编程技术网

Python 如何从脚本中的特定步骤开始?

Python 如何从脚本中的特定步骤开始?,python,Python,我有一个python脚本,它执行10个相互跟随的函数,我想让用户能够在特定函数开始执行脚本 例如: python main.py--start=function4 脚本只执行function4和流程中的下一个函数 我使用getopt 谢谢你的帮助。你可以这样做 if start >= 1: function1() if start >= 2: function2() if start >= 3: function3() 或者有一个功能列表: f = [

我有一个python脚本,它执行10个相互跟随的函数,我想让用户能够在特定函数开始执行脚本

例如:

python main.py--start=function4

脚本只执行function4和流程中的下一个函数

我使用getopt

谢谢你的帮助。

你可以这样做

if start >= 1:
    function1()
if start >= 2:
    function2()
if start >= 3:
    function3()
或者有一个功能列表:

f = [None, function1, function2, function3, ...]
for f in f_list[start:]:
    f()

你可以用

if __name__=='__main__':
     call_desiredfunction()
例如

def c():
    print 'c'

def b():
    print 'b'
    c()

def a():
    print 'a'
    b()


if __name__=='__main__':
    print 'Stared Execution'
    a()
输出:

Stared Execution
a
b
c

今天我可以给你们提建议:

使用goto

安装软件包:

$ pip install http://entrian.com/goto/goto-1.0.tar.gz#goto
编写一些代码:

from goto import goto, label

label .getinput
i = raw_input("Enter either 'a', 'b' or 'c', or any other letter to quit: ")
if i in ('a', 'b', 'c'):
    goto *i
else:
    goto .quit

label .a
print "You typed 'a'"

label .b
print "You typed 'b'"

label .c
print "You typed 'c'"

label .quit
print "Finished\n"
然后运行它:

$ python gt.py 
Enter either 'a', 'b' or 'c', or any other letter to quit: b
You typed 'b'
You typed 'c'
Finished

警告:在实际使用日历之前,请先查看日历。顺便说一句,代码真的很有用。

先试试谷歌搜索,这是一个相当基本的方法,一个教程可能会对你有更好的帮助。我试过了,我知道这个问题的所有答案,但我正在寻找一种更“性感”的方法D