功能原型? [英] function prototyping?

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

问题描述




有没有办法在python中''原型''函数,就像你在
C中那样?这就是''全球''关键词的用途,还是有更多优雅或''pythonic''方式进行前瞻性参考?


-

burton samograd kruhft .at。 gmail

kruhft.blogspot.com www.myspace.com/kruhft metashell.blogspot.com

解决方案

Burton Samograd写道:

有没有办法''原型' '在python中的功能,就像你在C中那样?这就是''全球''关键词的用途,还是有更优雅或''pythonic''的方式来做前瞻性参考?



有没有在Python中,它确实是一个前瞻性参考。永远

记住''def''和''class''是可执行的陈述:


def a():

b()


def b():

print" b called"


a()


只要在调用第一个

函数之前执行了两个def语句,它就会找到第二个。在上面的例子中,如果你在执行''def b()''之前调用了

a()那么函数''b''就不存在所以你

无法调用它。


Duncan Booth< du ********** @ invalid.invalid>写道:

Burton Samograd写道:

有没有办法在python中''原型''函数,就像在
C中那样?这就是''全球''关键词的用途,还是有更优雅或''pythonic''方式进行前瞻性参考?


实际上并非如此像Python中的前向引用这样的东西。永远记住''def''和''class''是可执行的语句:




好​​的,我们会在这里'是我的我想做。我有一个字典,我想要在模块文件config.py中初始化$


- config.py ------- ------------------

全球a_fun,b_fun

dict = {

' 'a'':a_fun,

''b'':b_fun

}

----------- ---------------------------


其中a_fun和b_fun在fun.py中:


- fun.py ----------------------------

def a_fun():传递

def b_fun():传递


导入配置

def main():

config.dict [''a'']()

config.dict [''b'']()

main()

--------------------------------------


我喜欢使用配置

变量进行模块/命名空间分离,但我想在

中轻松(重新)定义它们用户配置文件。 python是否有一个''弱''

参考或懒惰风格评估dict的定义在

上面的配置文件,所以我可以实现我的想法我试着这么做?

-

burton samograd kruhft .at。 gmail

kruhft.blogspot.com www.myspace.com/kruhft metashell.blogspot.com


Burton Samograd写道:

Duncan Booth< du ********** @ invalid.invalid>写道:

Burton Samograd写道:

有没有办法在python中''原型''函数,就像你一样会在C?那就是''全球''关键词的用途,还是有更优雅或''pythonic''的前瞻性参考方式?

真的没有像Python中的前向引用这样的东西。永远记住''def''和''class''是可执行的语句:



好​​的,我们会在这里'我正在尝试去做。我有一个字典,我想在模块文件config.py中初始化:

- config.py --------------- ----------
全球a_fun,b_fun
dict = {




不要使用''dict''作为标识符,它会影响内置字典类型。

''a'':a_fun,
''b'':b_fun
}
----- ---------------------------------

其中a_fun和b_fun在fun.py中:

- fun.py ----------------------------
def a_fun() :传递
def b_fun():传递


直到这一点,一切都(差不多)好了。你只需要

重写config.py所以从fun.py导入a_fun和b_fun:


# - config.py - -----------------------

导入有趣

conf = {

''a'':fun.a_fun,

''b'':fun.b_fun

}

#--- -----------------------------------


但是,我们有这个:

导入配置


然后我们有一个循环导入...


*但是*是否有必要将main()放在定义

a_fun和b_fun的同一个文件中?对于main()使用

不同的文件是很常见的(而且不仅仅是在Python中)。所以你可以通过

将fun.py分成fun.py和main.py来轻松解决你的问题:


# - main.py --- ----------------------

导入配置

def main(* args):

config.dict [''a'']()

config.dict [''b'']()


#这里我们有一个python技巧:

if __name__ ==''__ main__'':

import sys

sys.exit(main(* sys.argv [1:])

#--------------------------------- -----


我喜欢使用配置变量进行模块/命名空间分离但是我想在
用户的配置文件。


你可能想查看一个现有的配置模块。

python是否有一个弱的想法''
参考




是的,但那是完全不同的东西。


(剪辑)


HTH

-

bruno desthuilliers

python -c" print''@''。join([''。''。join([w [:: - 1] for w in p。拆分(''。'')])

p in''o **** @ xiludom.gro''。split(''''')])"


Hi,

Is there any way to ''prototype'' functions in python, as you would in
C? Would that be what the ''global'' keyword is for, or is there a more
elegant or ''pythonic'' way of doing forward references?

--
burton samograd kruhft .at. gmail
kruhft.blogspot.com www.myspace.com/kruhft metashell.blogspot.com

解决方案

Burton Samograd wrote:

Is there any way to ''prototype'' functions in python, as you would in
C? Would that be what the ''global'' keyword is for, or is there a more
elegant or ''pythonic'' way of doing forward references?


There isn''t really such a thing as a forward reference in Python. Always
remember that ''def'' and ''class'' are executable statements:

def a():
b()

def b():
print "b called"

a()

So long as you have executed both def statements before you call the first
function it will find the second one. In the example above, if you called
a() before executing ''def b()'' the function ''b'' wouldn''t exist so you
couldn''t call it.


Duncan Booth <du**********@invalid.invalid> writes:

Burton Samograd wrote:

Is there any way to ''prototype'' functions in python, as you would in
C? Would that be what the ''global'' keyword is for, or is there a more
elegant or ''pythonic'' way of doing forward references?


There isn''t really such a thing as a forward reference in Python. Always
remember that ''def'' and ''class'' are executable statements:



Ok, we''ll here''s what I''m trying to do. I have a dictionary that I
would like to initialize in a module file config.py:

-- config.py -------------------------
global a_fun, b_fun
dict = {
''a'': a_fun,
''b'': b_fun
}
--------------------------------------

where a_fun and b_fun are in fun.py:

-- fun.py ----------------------------
def a_fun(): pass
def b_fun(): pass

import config
def main():
config.dict[''a'']()
config.dict[''b'']()
main()
--------------------------------------

I like having the module/namespace seperation with the configuration
variables but I would like to make them easily (re)defined in the
configuration file by the user. Does python have the idea of a ''weak''
reference or lazy style evaluation for the definition of the dict in
the config file above so I can achive what i''m tryin to do?
--
burton samograd kruhft .at. gmail
kruhft.blogspot.com www.myspace.com/kruhft metashell.blogspot.com


Burton Samograd wrote:

Duncan Booth <du**********@invalid.invalid> writes:

Burton Samograd wrote:

Is there any way to ''prototype'' functions in python, as you would in
C? Would that be what the ''global'' keyword is for, or is there a more
elegant or ''pythonic'' way of doing forward references?

There isn''t really such a thing as a forward reference in Python. Always
remember that ''def'' and ''class'' are executable statements:


Ok, we''ll here''s what I''m trying to do. I have a dictionary that I
would like to initialize in a module file config.py:

-- config.py -------------------------
global a_fun, b_fun
dict = {



dont use ''dict'' as an identifier, it shadows the builtin dict type.
''a'': a_fun,
''b'': b_fun
}
--------------------------------------

where a_fun and b_fun are in fun.py:

-- fun.py ----------------------------
def a_fun(): pass
def b_fun(): pass
Until this point, everything is (almost) fine. You''d just need to
rewrite config.py so it imports a_fun and b_fun from fun.py:

#-- config.py -------------------------
import fun
conf = {
''a'': fun.a_fun,
''b'': fun.b_fun
}
# --------------------------------------

But then, we have this :
import config
And then we have a circular import...

*But* is it necessary to have the main() in the same file that defines
a_fun and b_fun ? It''s quite common (and not only in Python) to use a
distinct file for the main(). So you can easily solve your problem by
splitting fun.py into fun.py and main.py:

#-- main.py -------------------------
import config
def main(*args):
config.dict[''a'']()
config.dict[''b'']()

# here we have a python trick:
if __name__ == ''__main__'':
import sys
sys.exit(main(*sys.argv[1:])
# --------------------------------------

I like having the module/namespace seperation with the configuration
variables but I would like to make them easily (re)defined in the
configuration file by the user.
You may want to look at one of the existing configuration modules.
Does python have the idea of a ''weak''
reference



Yes, but that''s something totally different.

(snip)

HTH
--
bruno desthuilliers
python -c "print ''@''.join([''.''.join([w[::-1] for w in p.split(''.'')]) for
p in ''o****@xiludom.gro''.split(''@'')])"


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

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