code – Tools for representing JavaScript code (original) (raw)
Toggle table of contents sidebar
Tools for representing JavaScript code in BSON.
class bson.code.Code(code, scope=None, **kwargs)¶
Bases: str
BSON’s JavaScript code type.
Raises TypeError if code is not an instance ofstr or scope is not None
or an instance of dict.
Scope variables can be set by passing a dictionary as the scopeargument or by using keyword arguments. If a variable is set as a keyword argument it will override any setting for that variable in the scope dictionary.
Parameters:
- code (Union [_str,_ Code]) – A string containing JavaScript code to be evaluated or another instance of Code. In the latter case, the scope of code becomes this Code’s scope.
- scope (Optional [_ _Mapping_ _[_str,_ Any ] ]) – dictionary representing the scope in whichcode should be evaluated - a mapping from identifiers (as strings) to values. Defaults to
None
. This is applied after any scope associated with a given code above. - kwargs (Any) – scope variables can also be passed as keyword arguments. These are applied after scope and code.
Return type:
Changed in version 3.4: The default value for scope is None
instead of {}
.
property scope_: Mapping[str, Any] | None_¶
Scope dictionary for this instance or None
.