在运行时从文件加载函数

编程入门 行业动态 更新时间:2024-10-16 02:28:28
本文介绍了在运行时从文件加载函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好! 我想在运行时读取文件,其中包含普通的 Python函数定义,然后调用这些函数通过他们的 字符串名称。换句话说,我想用 函数定义读取任意文件,使用典型的''open()''调用,但是然后有 那些可以使用的功能。 ''import''关键字不合适,AFAIK,因为我想要 能够打开任何文件,而不是我提前知道的一个(因此可以在设计时导入)。 我已经有一些代码,我从许多新闻组拼凑而成 帖子,但我想知道是否有更清洁/更简单的方法。 以下是我的一个玩具示例到目前为止。我有一个名为''bar.txt'的文件 ,它包含两个函数定义。我有一个名为''foo.py''的主要 驱动程序,它读入''bar.txt''(但应该是 能够读取任何文件)之前没见过,然后调用''bar.txt''中指定的两个 函数。 ===== [bar .txt] ===== def negate(x): 返回-x def square(x): 返回x * x ===== [foo.py] ===== #open函数文件 foo_file = open(" bar.txt") foo_lines = foo_file.readlines() foo_file。 close() foo_str ="" .join(foo_lines) #编译代码 foo_code = compile(foo_str ,"< string>"," exec") foo_ns = {} exec(foo_code)in foo_ns #使用函数 k = 5 print foo_ns [" negate]](k)//输出-5 print foo_ns [" square"](k)//输出25 我不确定在表面下发生了什么,但是我猜测''compile()''和''exec()''命令加载了 ''negate()''和''square()'' 作为''foo.py''全局范围内的函数。我发现当我从一个函数中运行 ''compile()''和''exec()''时,说''f()'', $ b我从''bar.txt'读入的$ b函数不再可访问,因为它们是在全局范围内,而不在''f()''的范围内。 任何指针都会非常受欢迎。 谢谢! 布莱恩特

Hello! I would like to read in files, during run-time, which contain plain Python function definitions, and then call those functions by their string name. In other words, I''d like to read in arbitrary files with function definitions, using a typical ''open()'' call, but then have those functions available for use. The ''import'' keyword is not appropriate, AFAIK, because I want to be able to open any file, not one that I know ahead of time (and thus can import at design-time). I already have some code that I cobbled together from many newsgroup posts, but I wonder if there''s a cleaner/simpler way to do it. The following is a toy example of what I''m doing so far. I have a file called ''bar.txt'' that contains two function definitions. I have a main driver program called ''foo.py'' which reads in ''bar.txt'' (but should be able to read any file it hasn''t seen before), then calls the two functions specified in ''bar.txt''. ===== [bar.txt] ===== def negate(x): return -x def square(x): return x*x ===== [foo.py] ===== # open functions file foo_file = open("bar.txt") foo_lines = foo_file.readlines() foo_file.close() foo_str = "".join(foo_lines) # compile code foo_code = compile(foo_str, "<string>", "exec") foo_ns = {} exec(foo_code) in foo_ns # use functions k = 5 print foo_ns["negate"](k) // outputs -5 print foo_ns["square"](k) // outputs 25 I''m not sure exactly what happens below the surface, but I''m guessing the ''compile()'' and ''exec()'' commands load in ''negate()'' and ''square()'' as functions in the global scope of ''foo.py''. I find that when I run ''compile()'' and ''exec()'' from within a function, say ''f()'', the functions I read in from ''bar.txt'' are no longer accessible since they are in global scope, and not in the scope of ''f()''. Any pointers would be very welcome. Thanks! Bryant

推荐答案

2005-02-11,Bryant Huang< 73 **** @ gmail>写道: On 2005-02-11, Bryant Huang <73****@gmail> wrote: 我想读取文件,在运行时,包含普通的Python函数定义,然后用字符串调用那些函数名称。换句话说,我想用函数定义读取任意文件,使用典型的'open()''调用,但然后将这些函数用于以供使用。 这很简单: I would like to read in files, during run-time, which contain plain Python function definitions, and then call those functions by their string name. In other words, I''d like to read in arbitrary files with function definitions, using a typical ''open()'' call, but then have those functions available for use. that''s pretty simple:

cat foo.txt def foo(): print" foo here" def bar(): print" bar here" cat foo.txt def foo(): print "foo here" def bar(): print "bar here"

cat foo.py filename =''foo.txt'' execfile(filename) foo() bar() cat foo.py filename = ''foo.txt'' execfile(filename) foo() bar()

更多推荐

在运行时从文件加载函数

本文发布于:2023-11-07 23:14:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567696.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   加载   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!