加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl Inline :: Python模块,如何将代码放入字符串中

发布时间:2020-12-15 21:50:06 所属栏目:大数据 来源:网络整理
导读:我正在学习perl Inline :: Python库.在cpan网站的例子中,我们有 print "9 + 16 = ",add(9,16),"n"; print "9 - 16 = ",subtract(9,"n"; use Inline Python = 'END_OF_PYTHON_CODE'; def add(x,y): return x + y def subtract(x,y): return x - y END_OF_PY
我正在学习perl Inline :: Python库.在cpan网站的例子中,我们有
print "9 + 16 = ",add(9,16),"n";
   print "9 - 16 = ",subtract(9,"n";

   use Inline Python => <<'END_OF_PYTHON_CODE';
   def add(x,y): 
      return x + y

   def subtract(x,y):
      return x - y

   END_OF_PYTHON_CODE

是否可以将python代码放入字符串中,以便我可以在运行时创建python代码?例如,类似于:

my $python_code = "
def add(x,y):
   return x + y
";
print $python_code;
use Inline Python => "$python_code";
print "9 + 16 = ","n";

我们有一个项目将在运行时动态创建python函数.我们想要调用这些函数.是py_eval()的方法吗?提前致谢.

解决方法

没有使用Inline :: Python的经验,但使用Inline :: C you can use the bind function to set code at runtime,所以这可能会有效:
my $python_code = "
def add(x,y):
   return x + y
";
print $python_code;
Inline->bind( Python => $python_code );
print "9 + 16 = ","n";

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读