基于AScript的python3脚本语言发布啦!

AScript是一个开源的C#动态脚本解析执行引擎,支持扩展多种脚本语言,今天(2026年5月4日)正式发布了python3语言AScript.Lang.Python3的第1个版本0.0.1,快来试试吧!

一、介绍

支持python3基础语法、数据类型(int/float/bool/str/list/set/dict)、函数定义,以及列表、集合、字典操作。

二、安装

复制代码
1 install-package AScript
2 install-package AScript.Lang.Python3

三、使用说明

1、注册语言

复制代码
1 Script.Langs.Set("python3", Python3Lang.Instance);
2 // 可全局设置为默认语言
3 // Script.Langs.Set("python3", Python3Lang.Instance, setDefault: true);

2、上下文中指定语言

如果已全局设置默认语言则无需指定。

复制代码
1 var script = new Script();
2 script.Context.Langs = new [] { "python3" };
3 var s = @"
4 def sum(a,b):
5     return a+b
6 sum(10,20)
7 ";
8 Assert.AreEqual(30L, script.Eval(s));

3、使用@lang指定语言

复制代码
 1 var s = @"
 2 // 默认csharp语言
 3 int mult(int a, int b) => a*b;
 4 // 嵌入python3语言
 5 @lang python3
 6 def sum(a,b):
 7     return a+b
 8 @end
 9 int m = 10;
10 int n = 20;
11 mult(m, n) + sum(m, n);
12 ";
13 var script = new Script();
14 Assert.AreEqual(230, script.Eval(s));

4、字符串插值

复制代码
1 string s = @"
2 name='tom'; 
3 f'hello {name}, 5+8={5+8}'
4 ";
5 var script = new Script();
6 script.Context.Langs = new [] { "python3" };
7 Assert.AreEqual("hello tom, 5+8=13", script.Eval(s));

5、字符串索引和截取

复制代码
1 var script = new Script();
2 script.Context.Langs = new [] { "python3" };
3 Assert.AreEqual("e", script.Eval("'hello'[1]"));
4 Assert.AreEqual("e", script.Eval("'hello'[-4]"));
5 Assert.AreEqual("ell", script.Eval("'hello'[1:4]"));
6 Assert.AreEqual("ell", script.Eval("'hello'[-4:-1]"));

6、列表

复制代码
 1 var s = @"
 2 list1 = [1,2,3]
 3 list2 = [3,4,5]
 4 list3=list1 + list2
 5 ";
 6 var script = new Script();
 7 script.Context.Langs = new[] { "python3" };
 8 var result = script.Eval<List<object>>(s);
 9 Assert.AreEqual("1,2,3,3,4,5", string.Join(',', result));
10 Assert.AreEqual(2L, script.Eval("list3[1]"));
11 Assert.AreEqual(2L, script.Eval("list3[-5]"));

7、集合

集合元素是去重的。

复制代码
1 var script = new Script();
2 script.Context.Langs = new[] { "python3" };
3 var set = (HashSet<object>)script.Eval(@"
4 s = {1, 2, 2}
5 s.add(3)
6 s.add(2)
7 s
8 ");
9 Assert.AreEqual("1,2,3", string.Join(',', set));

8、字典

复制代码
 1 var s = @"
 2 p = {'name': '张三', 'age': 18}
 3 p['age']=20
 4 p
 5 ";
 6 var script = new Script();
 7 script.Context.Langs = new [] { "python3" };
 8 var dict = script.Eval<Dictionary<object, object>>(s);
 9 Assert.AreEqual(2, dict.Count);
10 Assert.AreEqual("张三", dict["name"]);
11 Assert.AreEqual(20L, dict["age"]);

9、for遍历值

复制代码
1 var code = @"
2 total = 0
3 for x in [1, 2, 3]:
4     total += x
5 total
6 ";
7 var script = new Script();
8 script.Context.Langs = new[] { "python3" };
9 Assert.AreEqual(6L, script.Eval(code));

10、for遍历值和索引

复制代码
1 var code = @"
2 result = ''
3 for i, x in enumerate([1, 2, 3]):
4     result += f'{i}:{x},'
5 result
6 ";
7 var script = new Script();
8 script.Context.Langs = new[] { "python3" };
9 Assert.AreEqual("0:1,1:2,2:3,", script.Eval(code));

10、列表推导式

复制代码
1 var code = @"[x * 2 for x in [1, 2, 3]]";
2 var script = new Script();
3 script.Context.Langs = new[] { "python3" };
4 var list = (List<object>)script.Eval(code);
5 Assert.AreEqual(3, list.Count);
6 Assert.AreEqual(2L, list[0]);
7 Assert.AreEqual(4L, list[1]);
8 Assert.AreEqual(6L, list[2]);

11、lambda

复制代码
1 string s = @"
2 f = lambda a,b: a+b
3 f(10,20)
4 ";
5 var script = new Script();
6 script.Context.Langs = new[] { "python3" };
7 Assert.AreEqual(30L, script.Eval(s));

12、类型注解

指定变量类型及函数返回值类型。

复制代码
 1 var s = @"
 2 @lang python3
 3 def sum(a:int,b:int)->int:
 4     return a+b
 5 m:int=10
 6 n:int=20
 7 sum(m,n)
 8 ";
 9 var script = new Script();
10 Assert.AreEqual(30L, script.Eval(s));

四、结束语

python3语法是参考https://www.runoob.com/python3/python3-tutorial.html教程来实现的,欢迎大家一起交流学习!


AScript开源地址:https://gitee.com/rockey627/AScript

相关推荐
EIP低代码平台5 分钟前
EIP低代码平台 - 应用管理 - 表单设计
低代码·c#·权限·工作流·netcore
czhc114007566312 分钟前
726:zoffset
c#
AI工具人PM产品经理14 分钟前
CSDN 自动发布全流程实战:从 CKEditor HTML 注入到创作页就地发布
python
_Jimmy_18 分钟前
Tool Calling 与 Function Calling 区别
人工智能·python·langchain
逆风飞翔的小叔32 分钟前
【Python基础】Python 流程控制语句使用详解
python·python 流程控制语句·python 流程控制·python 流程控制语句使用·python 流程控制语句详解
2601_9638702242 分钟前
基于Python的晋江文学城热门作品数据分析与可视化
开发语言·python·数据分析
llwszx1 小时前
【Java/Go后端手撸原生Agent(第七篇):Token预算管理 + 滑动窗口上下文裁剪】
java·后端·python·agent开发·上下文工程·上下文裁剪·滑动窗口裁剪
我怎么又饿了呀1 小时前
DataWhale—量化金融(task8 最大回撤 和 仓位管理)
python·金融·量化
Black_Rock_br1 小时前
打通 PyTorch Monarch 与 ROCm:单 Controller 架构的异构算力实战
人工智能·pytorch·python·开源
其实防守也摸鱼2 小时前
Kimi K3深度测评:长文本之外的真实力
运维·开发语言·网络·人工智能·python·学习·安全