python helper函数_使函数定义以python文件顺序独立

使函数定义以python文件顺序独立

我使用Python CGI。 我无法在定义函数之前调用它。

在Oracle PL / SQL中,存在“转发声明”的技巧:将所有函数都命名为最顶层,因此定义的顺序无关紧要。

Python中也有这样的技巧吗?

例:

def do_something(ds_parameter):

helper_function(ds_parameter)

....

def helper_function(hf_parameter):

....

def main():

do_something(my_value)

main()

大卫是对的,我的榜样是错误的。关于什么:

def do_something(ds_parameter):

helper_function(ds_parameter)

....

def print_something():

do_something(my_value)

print_something()

def helper_function(hf_parameter):

....

def main()

....

main()

我可以在脚本顶部“提前声明”功能吗?

user37986 asked 2020-07-21T01:23:45Z

7个解决方案

66 votes

在使用任何功能之前,必须先定义所有功能。

但是,只要可以在任何可执行代码使用功能之前定义所有功能,就可以按任何顺序定义功能。

您不需要“转发声明”,因为所有声明都是完全相互独立的。 只要所有声明都在所有可执行代码之前。

您有问题吗? 如果是这样,请发布无效的代码。

在您的示例中,print_something()不合适。

规则:所有功能必须在任何实际起作用的代码之前定义

因此,将所有起作用的语句放在最后。

S.Lott answered 2020-07-21T01:26:00Z

28 votes

更好地说明您的观点是:

def main():

print_something()

....

def do_something(ds_parameter):

helper_function(ds_parameter)

....

def print_something():

do_something(my_value)

def helper_function(hf_parameter):

....

main()

换句话说,您可以将main()的定义放在顶部,以方便编辑-如果大部分时间都花在编辑main上,则避免频繁滚动。

Stan Vernon answered 2020-07-21T01:26:25Z

3 votes

假设您有一些代码片段在定义函数后调用了main,那么您的示例将按编写的方式工作。 由于Python的解释方式,定义do_something函数时,不需要定义helper_function主体调用的任何函数。

Python在执行代码时将采取的步骤如下。

定义函数do_something。

定义函数helper_function。

定义函数main。

(基于上面的假设)致电main。

从主要,调用do_something。

从helper_function,调用helper_function。

Python唯一关心helper_function存在的时间是到了第六步。 在尝试查找helper_function以便调用它之前,您应该能够验证Python一直执行到第六步,然后引发错误。

David Locke answered 2020-07-21T01:27:20Z

2 votes

我从来没有遇到过需要“前向功能定义”的情况。您能不能简单地将do_something移到您的主要功能内部?

def do_something(ds_parameter):

helper_function(ds_parameter)

....

def print_something():

do_something(my_value)

def helper_function(hf_parameter):

....

def main()

print_something()

....

main()

Python不在乎在第3行上使用了helper_function()(在do_something函数中)

我建议使用WinPDB之类的代码并逐步执行代码。 它很好地显示了Python的parser / executor(?)的工作方式

dbr answered 2020-07-21T01:27:49Z

2 votes

def funB(d,c):

return funA(d,c)

print funB(2,3)

def funA(x,y):

return x+y

上面的代码将返回错误。 但是,下面的代码很好...

def funB(d,c):

return funA(d,c)

def funA(x,y):

return x+y

print funB(2,3)

因此,即使您必须在完成任何实际工作之前一定先定义函数,但如果不显式使用该函数,则有可能逃脱现实。 我认为这有点类似于其他语言的原型制作。

nyan314sn answered 2020-07-21T01:28:14Z

1 votes

对于所有人来说,尽管做法不好,但仍需要解决方法...

我有一个类似的问题,并这样解决:

import Configurations as this

'''Configurations.py'''

if __name__ == '__main__':

this.conf01()

'''Test conf'''

def conf01():

print ("working")

因此,我可以在文件顶部更改目标配置。 诀窍是将文件导入自身。

luke8800gts answered 2020-07-21T01:28:42Z

0 votes

使用多处理模块:

from multiprocessing import Process

p1 = Process(target=function_name, args=(arg1, arg2,))

p1.start()

Neeraj Bansal answered 2020-07-21T01:29:02Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值