@@ -13,9 +13,9 @@ class PythonFunctionTests: XCTestCase {
13
13
return
14
14
}
15
15
16
- let pythonAdd = PythonFunction { ( params : [ PythonObject ] ) in
17
- let lhs = params [ 0 ]
18
- let rhs = params [ 1 ]
16
+ let pythonAdd = PythonFunction { args in
17
+ let lhs = args [ 0 ]
18
+ let rhs = args [ 1 ]
19
19
return lhs + rhs
20
20
} . pythonObject
21
21
@@ -30,27 +30,25 @@ class PythonFunctionTests: XCTestCase {
30
30
return
31
31
}
32
32
33
- let constructor = PythonInstanceMethod { ( params: [ PythonObject ] ) in
34
- let `self` = params [ 0 ]
35
- let arg = params [ 1 ]
36
- `self`. constructor_arg = arg
33
+ let constructor = PythonInstanceMethod { args in
34
+ let `self` = args [ 0 ]
35
+ `self`. constructor_arg = args [ 1 ]
37
36
return Python . None
38
37
}
39
38
40
39
// Instead of calling `print`, use this to test what would be output.
41
40
var printOutput : String ?
42
41
43
- let displayMethod = PythonInstanceMethod { ( params : [ PythonObject ] ) in
44
- // let `self` = params[0]
45
- let arg = params [ 1 ]
46
- printOutput = String ( arg )
42
+ // Example of function using an alternative syntax for `args`.
43
+ let displayMethod = PythonInstanceMethod { ( args : [ PythonObject ] ) in
44
+ // let `self` = args[0 ]
45
+ printOutput = String ( args [ 1 ] )
47
46
return Python . None
48
47
}
49
48
50
- let classMethodOriginal = PythonInstanceMethod { ( params: [ PythonObject ] ) in
51
- // let cls = params[0]
52
- let arg = params [ 1 ]
53
- printOutput = String ( arg)
49
+ let classMethodOriginal = PythonInstanceMethod { args in
50
+ // let cls = args[0]
51
+ printOutput = String ( args [ 1 ] )
54
52
return Python . None
55
53
}
56
54
@@ -121,16 +119,17 @@ class PythonFunctionTests: XCTestCase {
121
119
members: [
122
120
" str_prefix " : " HelloException-prefix " ,
123
121
124
- " __init__ " : PythonInstanceMethod { ( params : [ PythonObject ] ) in
125
- let `self` = params [ 0 ]
126
- let message = " hello \( params [ 1 ] ) "
122
+ " __init__ " : PythonInstanceMethod { args in
123
+ let `self` = args [ 0 ]
124
+ let message = " hello \( args [ 1 ] ) "
127
125
helloOutput = String ( message)
128
126
129
127
// Conventional `super` syntax causes problems; use this instead.
130
128
Python . Exception. __init__ ( `self`, message)
131
129
return Python . None
132
130
} ,
133
131
132
+ // Example of function using the `self` convention instead of `args`.
134
133
" __str__ " : PythonInstanceMethod { ( `self`: PythonObject ) in
135
134
return `self`. str_prefix + Python. repr ( `self`)
136
135
}
@@ -143,18 +142,19 @@ class PythonFunctionTests: XCTestCase {
143
142
members: [
144
143
" str_prefix " : " HelloWorldException-prefix " ,
145
144
146
- " __init__ " : PythonInstanceMethod { ( params : [ PythonObject ] ) in
147
- let `self` = params [ 0 ]
148
- let message = " world \( params [ 1 ] ) "
145
+ " __init__ " : PythonInstanceMethod { args in
146
+ let `self` = args [ 0 ]
147
+ let message = " world \( args [ 1 ] ) "
149
148
helloWorldOutput = String ( message)
150
149
151
- `self`. int_param = params [ 2 ]
150
+ `self`. int_param = args [ 2 ]
152
151
153
152
// Conventional `super` syntax causes problems; use this instead.
154
153
HelloException . __init__ ( `self`, message)
155
154
return Python . None
156
155
} ,
157
156
157
+ // Example of function using the `self` convention instead of `args`.
158
158
" custom_method " : PythonInstanceMethod { ( `self`: PythonObject ) in
159
159
return `self`. int_param
160
160
}
@@ -178,7 +178,9 @@ class PythonFunctionTests: XCTestCase {
178
178
179
179
// Test that subclasses behave like Python exceptions
180
180
181
- let testFunction = PythonFunction { ( _: [ PythonObject ] ) in
181
+ // Example of function with no named parameters, which can be stated
182
+ // ergonomically using an underscore. The ignored input is a [PythonObject].
183
+ let testFunction = PythonFunction { _ in
182
184
throw HelloWorldException ( " EXAMPLE ERROR MESSAGE " , 2 )
183
185
} . pythonObject
184
186
0 commit comments