You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The special function call `pythonjs.configure` can be inserted anywhere in your code to turn off an on dynamic
319
+
features of the language.
320
+
321
+
If the option `direct_keys` is True then dictionary key lookups are done directly (faster),
322
+
objects can not be used as keys, only strings and numbers can then be used as dictionary keys.
323
+
324
+
The option `direct_operator` controls operator overloading for a given operator: '+', '*'.
325
+
If '+' is declared a direct operator then `__add__` overload methods are not called,
326
+
the operands are always assumed to be compatible with javascript addition.
327
+
328
+
The option `runtime_exceptions` if False disables extra runtime checking of expressions and assignments,
329
+
note this is always False in javascript mode.
330
+
331
+
```
332
+
pythonjs.configure(
333
+
javascript=True/False, ## default False
334
+
runtime_exceptions=True/False, ## default True
335
+
direct_keys=True/False, ## default False
336
+
direct_operator=string ## default 'None'
337
+
)
338
+
```
339
+
340
+
Gotchas
341
+
---------
342
+
1. The calling context of `this` must be taken into account when using fast javascript mode, code that comes after: `pythonjs.configure(javascript=True)` or is inside a `with javascript:` block. When in javascript mode, passing a method as a callback, or setting it as an attribute on another object, requires you call `f.bind(self)` to ensure that `self` within the method points to the class instance. This is not required when using classes defined normal mode, because the `this` calling context is automatically managed.
343
+
344
+
```
345
+
class A:
346
+
def method(self):
347
+
print(self)
348
+
349
+
a = A()
350
+
351
+
with javascript:
352
+
class B:
353
+
def method(self):
354
+
print(self)
355
+
356
+
b = B()
357
+
a.b_method1 = b.method
358
+
a.b_method2 = b.method.bind(b)
359
+
360
+
a.method() ## OK: prints a
361
+
a.b_method1() ## FAILS: prints a, should have printed b
362
+
a.b_method2() ## OK: prints b
363
+
364
+
b.a_method = a.method
365
+
b.a_method() ## OK: prints a
366
+
367
+
```
368
+
369
+
2. When using direct operators, builtins are also affected. List + list will no longer return a new array of items from both lists. String * N will no longer return the string multipled by the number.
370
+
371
+
```
372
+
a = [1,2] + [3,4] ## OK: a is [1,2,3,4]
373
+
pythonjs.configure(direct_operator="+")
374
+
b = [1,2] + [3,4] ## FAILS
375
+
376
+
c = "HI" * 2 ## OK: c is "HIHI"
377
+
pythonjs.configure(direct_operator="*")
378
+
d = "HI" * 2 ## FAILS
379
+
380
+
```
381
+
382
+
3. The syntax `from mymodule import *` allows you to import another python script from the same folder,
383
+
but both mymodule and the parent will share the same namespace, mymodule can use global variables defined in the parent.
0 commit comments