Skip to content

Commit 7e89629

Browse files
authored
python 调整泛型使用 __n_n (#2294)
* python 调整泛型使用 `__n` 为 `_n` * 调整泛型使用方式用例 * 更新文档 * GenericTest
1 parent 2217013 commit 7e89629

File tree

6 files changed

+37
-37
lines changed

6 files changed

+37
-37
lines changed

doc/unity/en/tutorial/python2cs.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Calling C# from Python
1+
# Calling C# from Python
22

33
> 💡 PuerTS 3.0 also supports [JavaScript](./js2cs.md) and [Lua](./lua2cs.md) calling C#, each with different syntax. Click the links to see the corresponding tutorials.
44
@@ -181,7 +181,7 @@ void Start() {
181181
env.Eval(@"
182182
exec('''
183183
import System
184-
import System.Collections.Generic.List__T1 as List
184+
import System.Collections.Generic.List_1 as List
185185
186186
# create generic type: List<int>
187187
ListInt = puerts.generic(List, System.Int32)
@@ -197,9 +197,9 @@ print(ls.Count) # 3
197197
}
198198
```
199199

200-
> ⚠️ **Special naming for generic types**: In C#, generic type names use backticks to indicate the number of type parameters (e.g. `` List`1 ``). In Python's `import` syntax, backticks must be replaced with `__T` followed by the count:
201-
> - `` List`1 ```List__T1`
202-
> - `` Dictionary`2 ```Dictionary__T2`
200+
> ⚠️ **Special naming for generic types**: In C#, generic type names use backticks to indicate the number of type parameters (e.g. `` List`1 ``). In Python's `import` syntax, backticks must be replaced with `_` followed by the count:
201+
> - `` List`1 ```List_1`
202+
> - `` Dictionary`2 ```Dictionary_2`
203203
>
204204
> When using `puerts.load_type()`, you can use the original backtick format directly:
205205
> ```python
@@ -225,12 +225,12 @@ x = InnerClassA()
225225
print(x.Foo) # Hello
226226
227227
# access generic nested class
228-
InnerClassB_T1 = puerts.generic(TestNestedTypes.InnerClassB__T1, Int32)
229-
y = InnerClassB_T1()
228+
InnerClassB_1 = puerts.generic(TestNestedTypes.InnerClassB_1, Int32)
229+
y = InnerClassB_1()
230230
print(y.Bar) # Hello
231231
232-
InnerClassB_T2 = puerts.generic(TestNestedTypes.InnerClassB__T2, Int32, String)
233-
z = InnerClassB_T2()
232+
InnerClassB_2 = puerts.generic(TestNestedTypes.InnerClassB_2, Int32, String)
233+
z = InnerClassB_1()
234234
print(z.Bar) # Hello
235235
''')
236236
");

doc/unity/zhcn/tutorial/python2cs.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 在 Python 中调用 C#
1+
# 在 Python 中调用 C#
22

33
> 💡 PuerTS 3.0 同时支持 [JavaScript](./js2cs.md)[Lua](./lua2cs.md) 调用 C#,语法各有不同,可点击链接查看对应教程。
44
@@ -181,7 +181,7 @@ void Start() {
181181
env.Eval(@"
182182
exec('''
183183
import System
184-
import System.Collections.Generic.List__T1 as List
184+
import System.Collections.Generic.List_1 as List
185185
186186
# create generic type: List<int>
187187
ListInt = puerts.generic(List, System.Int32)
@@ -206,11 +206,11 @@ void Start() {
206206
var env = new Puerts.ScriptEnv(new Puerts.BackendPython());
207207
env.Eval(@"
208208
exec('''
209-
from System.Collections.Generic import List__T1
209+
from System.Collections.Generic import List_1
210210
from System import Int32
211211
212212
# use square bracket syntax to create generic type: List<int>
213-
List_Int32 = List__T1[Int32]
213+
List_Int32 = List_1[Int32]
214214
ls = List_Int32()
215215
ls.Add(1)
216216
ls.Add(2)
@@ -223,11 +223,11 @@ print(ls.Count) # 3
223223
}
224224
```
225225

226-
中括号语法 `List__T1[Int32]` 等价于 `puerts.generic(List__T1, Int32)`,对于多个类型参数,使用逗号分隔:`Dictionary__T2[String, Int32]`
226+
中括号语法 `List_1[Int32]` 等价于 `puerts.generic(List_1, Int32)`,对于多个类型参数,使用逗号分隔:`Dictionary_2[String, Int32]`
227227

228-
> ⚠️ **泛型类名的特殊表示**:C# 中泛型类名使用反引号表示类型参数个数(如 `` List`1 ``),而在 Python 的 `import` 语法中,反引号需要替换为 `__T` 加参数个数:
229-
> - `` List`1 ```List__T1`
230-
> - `` Dictionary`2 ```Dictionary__T2`
228+
> ⚠️ **泛型类名的特殊表示**:C# 中泛型类名使用反引号表示类型参数个数(如 `` List`1 ``),而在 Python 的 `import` 语法中,反引号需要替换为 `_` 加参数个数:
229+
> - `` List`1 ```List_1`
230+
> - `` Dictionary`2 ```Dictionary_2`
231231
>
232232
> 使用 `puerts.load_type()` 时,可以直接使用反引号原始格式:
233233
> ```python
@@ -253,12 +253,12 @@ x = InnerClassA()
253253
print(x.Foo) # Hello
254254
255255
# access generic nested class
256-
InnerClassB_T1 = puerts.generic(TestNestedTypes.InnerClassB__T1, Int32)
257-
y = InnerClassB_T1()
256+
InnerClassB_1 = puerts.generic(TestNestedTypes.InnerClassB_1, Int32)
257+
y = InnerClassB_1()
258258
print(y.Bar) # Hello
259259
260-
InnerClassB_T2 = puerts.generic(TestNestedTypes.InnerClassB__T2, Int32, String)
261-
z = InnerClassB_T2()
260+
InnerClassB_2 = puerts.generic(TestNestedTypes.InnerClassB_2, Int32, String)
261+
z = InnerClassB_2()
262262
print(z.Bar) # Hello
263263
''')
264264
");
@@ -438,10 +438,10 @@ void Start() {
438438
var env = new Puerts.ScriptEnv(new Puerts.BackendPython());
439439
env.Eval(@"
440440
exec('''
441-
from System.Collections.Generic import List__T1
441+
from System.Collections.Generic import List_1
442442
from System import Int32
443443
444-
List_Int32 = List__T1[Int32]
444+
List_Int32 = List_1[Int32]
445445
myList = List_Int32()
446446
myList.Add(1)
447447
myList.Add(2)

unity/test/Src/Cases/Python/CrossLang/GenericTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public void ListRangePythonTest()
4848
var pythonEnv = new ScriptEnv(new BackendPython());
4949
pythonEnv.Eval(@"
5050
exec('''
51-
import System.Collections.Generic.List__T1 as List
51+
import System.Collections.Generic.List_1 as List_1
5252
import System
53-
ListInt = puerts.generic(List, System.Int32)
53+
ListInt = puerts.generic(List_1, System.Int32)
5454
ls = ListInt()
5555
ls.Add(1)
5656
ls.Add(2)

unity/test/Src/Cases/Python/GenIteratorTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ public void GenIteratorTest()
1515

1616
pythonEnv.Eval(@"
1717
exec('''
18-
from System.Collections.Generic import List__T1
18+
from System.Collections.Generic import List_1
1919
from System import Int32
20-
List__Int32 = List__T1[Int32]
21-
myList = List__Int32()
20+
List_Int32 = List_1[Int32]
21+
myList = List_Int32()
2222
myList.Add(1)
2323
myList.Add(2)
2424
myList.Add(3)

unity/test/Src/Cases/Python/NestedTypesTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ from Puerts.UnitTest import TestNestedTypes
6060
assertAndPrint = TestHelper.AssertAndPrint
6161
InnerClassA = TestNestedTypes.InnerClassA
6262
InnerClassB = TestNestedTypes.InnerClassB
63-
InnerClassB__T1 = puerts.generic(TestNestedTypes.InnerClassB__T1, Int32)
64-
InnerClassB__T2 = puerts.generic(TestNestedTypes.InnerClassB__T2, Int32, String)
63+
InnerClassB_1 = puerts.generic(TestNestedTypes.InnerClassB_1, Int32)
64+
InnerClassB_2 = puerts.generic(TestNestedTypes.InnerClassB_2, Int32, String)
6565
x = InnerClassA()
6666
o = InnerClassB()
67-
y = InnerClassB__T1()
68-
z = InnerClassB__T2()
67+
y = InnerClassB_1()
68+
z = InnerClassB_2()
6969
''')
7070
");
7171
pythonEnv.Dispose();

unity/upms/python/Runtime/Src/Backends/BackendPython.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,12 @@ class puerts:
131131
def load_type(type_name: str):
132132
""""""
133133
Load a C# class or generic type definition, raise ModuleNotFoundError if the type cannot be found or loaded.
134-
:param type_name: The full name of the C# type to load. If the type is generic, use the format 'TypeName__Tn' where n is the number of generic parameters.
134+
:param type_name: The full name of the C# type to load. If the type is generic, use the format 'TypeName_n' where n is the number of generic parameters.
135135
:return: The loaded C# class or generic type definition
136136
""""""
137-
generic_tick_index = type_name.find('__T')
137+
generic_tick_index = type_name.find('_')
138138
if generic_tick_index != -1:
139-
suffix = type_name[generic_tick_index + 3:]
139+
suffix = type_name[generic_tick_index + 1:]
140140
if suffix and suffix.isdigit():
141141
type_name = type_name[:generic_tick_index] + '`' + suffix
142142
if type_name in _csTypeCache_:
@@ -157,9 +157,9 @@ raise ModuleNotFoundError(f'Failed to load type {type_name} in loadType')
157157
for i in range(nestedTypes.Length):
158158
ntype = nestedTypes.get_Item(i)
159159
if ntype.IsGenericTypeDefinition:
160-
nName = ntype.Name ## convert name (T`1) to (T__T1) for syntax compatibility
160+
nName = ntype.Name ## convert name (T`1) to (T_1) for syntax compatibility
161161
tick_index = nName.find('`')
162-
nName = nName[:tick_index] + '__T' + nName[tick_index + 1:]
162+
nName = nName[:tick_index] + '_' + nName[tick_index + 1:]
163163
setattr(cs_class, nName, puerts.load_type(ntype.FullName))
164164
pass ## skip generic type definitions, use puerts.generic to instantiate them
165165
else:

0 commit comments

Comments
 (0)