@@ -13,12 +13,12 @@ class PythonFunctionTests: XCTestCase {
13
13
return
14
14
}
15
15
16
- let pythonAdd = PythonFunction { ( args: [ PythonObject ] ) in
16
+ let pythonAdd = PythonFunction { args in
17
17
let lhs = args [ 0 ]
18
18
let rhs = args [ 1 ]
19
19
return lhs + rhs
20
20
} . pythonObject
21
-
21
+
22
22
let pythonSum = pythonAdd ( 2 , 3 )
23
23
XCTAssertNotNil ( Double ( pythonSum) )
24
24
XCTAssertEqual ( pythonSum, 5 )
@@ -54,35 +54,33 @@ class PythonFunctionTests: XCTestCase {
54
54
return
55
55
}
56
56
57
- let constructor = PythonInstanceMethod { ( args: [ PythonObject ] ) in
57
+ let constructor = PythonInstanceMethod { args in
58
58
let `self` = args [ 0 ]
59
- let arg = args [ 1 ]
60
- `self`. constructor_arg = arg
59
+ `self`. constructor_arg = args [ 1 ]
61
60
return Python . None
62
61
}
63
-
62
+
64
63
// Instead of calling `print`, use this to test what would be output.
65
64
var printOutput : String ?
66
-
65
+
66
+ // Example of function using an alternative syntax for `args`.
67
67
let displayMethod = PythonInstanceMethod { ( args: [ PythonObject ] ) in
68
- // let `self` = params[0]
69
- let arg = args [ 1 ]
70
- printOutput = String ( arg)
68
+ // let `self` = args[0]
69
+ printOutput = String ( args [ 1 ] )
71
70
return Python . None
72
71
}
73
-
74
- let classMethodOriginal = PythonInstanceMethod { ( args: [ PythonObject ] ) in
75
- // let cls = params[0]
76
- let arg = args [ 1 ]
77
- printOutput = String ( arg)
72
+
73
+ let classMethodOriginal = PythonInstanceMethod { args in
74
+ // let cls = args[0]
75
+ printOutput = String ( args [ 1 ] )
78
76
return Python . None
79
77
}
80
-
78
+
81
79
// Did not explicitly convert `constructor` or `displayMethod` to
82
80
// PythonObject. This is intentional, as the `PythonClass` initializer
83
81
// should take any `PythonConvertible` and not just `PythonObject`.
84
82
let classMethod = Python . classmethod ( classMethodOriginal. pythonObject)
85
-
83
+
86
84
let Geeks = PythonClass ( " Geeks " , members: [
87
85
// Constructor
88
86
" __init__ " : constructor,
@@ -100,10 +98,10 @@ class PythonFunctionTests: XCTestCase {
100
98
XCTAssertEqual ( obj. constructor_arg, " constructor argument " )
101
99
XCTAssertEqual ( obj. string_attribute, " Geeks 4 geeks! " )
102
100
XCTAssertEqual ( obj. int_attribute, 1706256 )
103
-
101
+
104
102
obj. func_arg ( " Geeks for Geeks " )
105
103
XCTAssertEqual ( printOutput, " Geeks for Geeks " )
106
-
104
+
107
105
Geeks . class_func ( " Class Dynamically Created! " )
108
106
XCTAssertEqual ( printOutput, " Class Dynamically Created! " )
109
107
}
@@ -136,16 +134,16 @@ class PythonFunctionTests: XCTestCase {
136
134
137
135
var helloOutput : String ?
138
136
var helloWorldOutput : String ?
139
-
137
+
140
138
// Declare subclasses of `Python.Exception`
141
-
139
+
142
140
let HelloException = PythonClass (
143
141
" HelloException " ,
144
142
superclasses: [ Python . Exception] ,
145
143
members: [
146
144
" str_prefix " : " HelloException-prefix " ,
147
145
148
- " __init__ " : PythonInstanceMethod { ( args: [ PythonObject ] ) in
146
+ " __init__ " : PythonInstanceMethod { args in
149
147
let `self` = args [ 0 ]
150
148
let message = " hello \( args [ 1 ] ) "
151
149
helloOutput = String ( message)
@@ -155,6 +153,7 @@ class PythonFunctionTests: XCTestCase {
155
153
return Python . None
156
154
} ,
157
155
156
+ // Example of function using the `self` convention instead of `args`.
158
157
" __str__ " : PythonInstanceMethod { ( `self`: PythonObject ) in
159
158
return `self`. str_prefix + Python. repr ( `self`)
160
159
}
@@ -167,7 +166,7 @@ class PythonFunctionTests: XCTestCase {
167
166
members: [
168
167
" str_prefix " : " HelloWorldException-prefix " ,
169
168
170
- " __init__ " : PythonInstanceMethod { ( args: [ PythonObject ] ) in
169
+ " __init__ " : PythonInstanceMethod { args in
171
170
let `self` = args [ 0 ]
172
171
let message = " world \( args [ 1 ] ) "
173
172
helloWorldOutput = String ( message)
@@ -179,14 +178,15 @@ class PythonFunctionTests: XCTestCase {
179
178
return Python . None
180
179
} ,
181
180
181
+ // Example of function using the `self` convention instead of `args`.
182
182
" custom_method " : PythonInstanceMethod { ( `self`: PythonObject ) in
183
183
return `self`. int_param
184
184
}
185
185
]
186
186
) . pythonObject
187
187
188
188
// Test that inheritance works as expected
189
-
189
+
190
190
let error1 = HelloException ( " test 1 " )
191
191
XCTAssertEqual ( helloOutput, " hello test 1 " )
192
192
XCTAssertEqual ( Python . str ( error1) , " HelloException-prefix HelloException('hello test 1') " )
@@ -201,8 +201,10 @@ class PythonFunctionTests: XCTestCase {
201
201
XCTAssertNotEqual ( error2. custom_method ( ) , " 123 " )
202
202
203
203
// Test that subclasses behave like Python exceptions
204
-
205
- let testFunction = PythonFunction { ( _: [ PythonObject ] ) in
204
+
205
+ // Example of function with no named parameters, which can be stated
206
+ // ergonomically using an underscore. The ignored input is a [PythonObject].
207
+ let testFunction = PythonFunction { _ in
206
208
throw HelloWorldException ( " EXAMPLE ERROR MESSAGE " , 2 )
207
209
} . pythonObject
208
210
0 commit comments