AScript.Lang.Python3
0.0.1
.NET Standard 2.0
This package targets .NET Standard 2.0. The package is compatible with this framework or higher.
.NET Framework 4.5
This package targets .NET Framework 4.5. The package is compatible with this framework or higher.
dotnet add package AScript.Lang.Python3 --version 0.0.1
NuGet\Install-Package AScript.Lang.Python3 -Version 0.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AScript.Lang.Python3" Version="0.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AScript.Lang.Python3" Version="0.0.1" />
<PackageReference Include="AScript.Lang.Python3" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AScript.Lang.Python3 --version 0.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: AScript.Lang.Python3, 0.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package AScript.Lang.Python3@0.0.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=AScript.Lang.Python3&version=0.0.1
#tool nuget:?package=AScript.Lang.Python3&version=0.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
AScript.Lang.Python3
介绍
python3语法
安装
install-package AScript
install-package AScript.Lang.Python3
使用说明
- 命名空间:using AScript.Lang.Python3;
- 已内置python3常用数据类型:int/float/bool/str/list/set/dict
注册python3语言
Script.Langs.Set("python3", Python3Lang.Instance);
// 可全局设置为默认语言
// Script.Langs.Set("python3", Python3Lang.Instance, setDefault: true);
上下文中指定python3语言
如果已全局设置默认语言则无需指定
var script = new Script();
script.Context.Langs = new [] { "python3" };
var s = @"
def sum(a,b):
return a+b
sum(10,20)
";
Assert.AreEqual(30L, script.Eval(s));
使用@lang指定python3语言
var s = @"
// 默认csharp语言
int mult(int a, int b) => a*b;
// 嵌入python3语言
@lang python3
def sum(a,b):
return a+b
@end
int m = 10;
int n = 20;
mult(m, n) + sum(m, n);
";
var script = new Script();
Assert.AreEqual(230, script.Eval(s));
字符串内插值
string s = @"
name='tom';
f'hello {name}, 5+8={5+8}'
";
var script = new Script();
script.Context.Langs = new [] { "python3" };
Assert.AreEqual("hello tom, 5+8=13", script.Eval(s));
字符串索引和截取
var script = new Script();
script.Context.Langs = new [] { "python3" };
Assert.AreEqual('e', script.Eval("'hello'[1]"));
Assert.AreEqual('e', script.Eval("'hello'[-4]"));
Assert.AreEqual("ell", script.Eval("'hello'[1:4]"));
Assert.AreEqual("ell", script.Eval("'hello'[-4:-1]"));
数组
var script = new Script();
script.Context.Langs = new [] { "python3" };
var result1 = (List<object>)script.Eval("var list1 = [0,1,2,3,4]; list1[1:4]");
var result2 = (List<object>)script.Eval("var list2 = [0,1,2,3,4]; list2[-4:-1]");
CollectionAssert.AreEqual(new List<object> { 1, 2, 3 }, result1);
CollectionAssert.AreEqual(new List<object> { 1, 2, 3 }, result2);
Assert.AreEqual(1, script.Eval("arr1[1]"));
Assert.AreEqual(1, script.Eval("arr1[-4]"));
字典
var s = @"
p = {'name': '张三', 'age': 18}
p['age']=20
p
";
var script = new Script();
script.Context.Langs = new [] { "python3" };
var dict = script.Eval<Dictionary<object, object>>(s);
Assert.AreEqual(2, dict.Count);
Assert.AreEqual("张三", dict["name"]);
Assert.AreEqual(20L, dict["age"]);
集合
集合会自动去重。
var s = @"
s = {1, 2}
s.add(3)
s.add(2)
s
";
var script = new Script();
script.Context.Langs = new [] { "python3" };
var set = script.Eval<HashSet<object>>(s);
Assert.AreEqual(3, set.Count);
for遍历值
var code = @"
total = 0
for x in [1, 2, 3]:
total += x
total
";
var script = new Script();
script.Context.Langs = new[] { "python3" };
Assert.AreEqual(6L, script.Eval(code));
for遍历值和索引
var code = @"
result = ''
for i, x in enumerate([1, 2, 3]):
result += f'{i}:{x},'
result
";
var script = new Script();
script.Context.Langs = new[] { "python3" };
Assert.AreEqual("0:1,1:2,2:3,", script.Eval(code));
列表推导式
var code = @"[x * 2 for x in [1, 2, 3]]";
var script = new Script();
script.Context.Langs = new[] { "python3" };
var list = (List<object>)script.Eval(code);
Assert.AreEqual(3, list.Count);
Assert.AreEqual(2L, list[0]);
Assert.AreEqual(4L, list[1]);
Assert.AreEqual(6L, list[2]);
lambda
string s = @"
f = lambda a,b: a+b
f(10,20)
";
var script = new Script();
script.Context.Langs = new[] { "python3" };
Assert.AreEqual(30L, script.Eval(s));
类型注解
指定变量类型及函数返回值类型。
var script = new Script();
script.Context.Langs = new [] { "python3" };
var s = @"
def sum(a:int,b:int)->int:
return a+b
m:int=10
n:int=20
sum(m,n)
";
Assert.AreEqual(30L, script.Eval(s));
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
| .NET Framework | net45 is compatible. net451 was computed. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.0.1 | 95 | 5/4/2026 |
基础语法