Keyword Args & Immutability
Using Python's keyword args and immutability with Transcrypt.
We'll cover the following...
Keyword arguments
By default, unpacking keyword arguments with **kwargs
in a function does not work out of the box in Transcrypt. This is because it would lead to bloated code when it gets transpiled to JavaScript. Fortunately, Transcrypt provides us with a compiler directive that will selectively enable that feature for just when it’s needed. Here is a simple example:
# __pragma__('kwargs')def test_function(**kwargs):for key, val in kwargs.items():print(key, ":", val)# __pragma__('nokwargs')
So, we basically just have to wrap our function that uses **kwargs
in the __pragma__
compiler directives to turn it on, then turn it off again. Transcrypt will then make sure that the function is transpiled into JavaScript with that capability enabled.
If we do try and use **kwargs
without the __pragma__
compiler directives, we might see an error similar to this in the web browser console at runtime:
ReferenceError: kwargs is not defined
...