import { PyObject } from "https://deno.land/x/python@0.3.1/src/python.ts";
Represents a Python object.
It can be anything, like an int, a string, a list, a dict, etc. and even a module itself.
Normally, you will deal with proxied PyObjects, which are basically JS objects but the get, set, etc. methods you perform on them are actually proxied to Python interpreter API.
In case you need access to actual PyObject (which this module does too,
internally), there's a Symbol on Proxied PyObjects ProxiedPyObject
that is exported from this module too. It contains reference to PyObject
.
Both proxied PyObject and normal PyObject implement some basic methods like
valueOf
, toString
and Deno inspect to provide pretty-printing, and also
a way to cast Python values as JS types using valueOf
. For caveats on valueOf
,
see its documentation.
Do not construct this manually, as it takes an Unsafe Pointer pointing to the C PyObject.
Constructors
Properties
Increases ref count of the object and returns it.
Creates proxy object that maps basic JS operations on objects such as gets, sets, function calls, has, etc. to Python interpreter API. This makes using Python APIs in JS less cumbersome.
Usually, you will deal with proxied PyObjects because they're easier to interact with.
If you somehow need the actual PyObject
, refer to it's documentation.
To keep it consistent, proxied objects' further get calls return proxy objects only,
so you can safely chain them. But for instance, if you made a call to a method that
returns a Python list using proxy object, you can call .valueOf()
on it to turn it into
a JS Array.
What you can do on proxy objects:
-
Call them, if they are a function. An error will be thrown otherwise.
-
Get their attributes. Such as get
lower
attribute on astr
object. This same thing is used to get values of given gets indict
s as well. But the thing is, preference is given to attributes, if its not found, then we try to look fordict
key. We could not differentiate normal property access like something.property withsomething[indexed]
in JS, so they are done on same thing. In case this is not viable for you, you can call theget
method on the proxy object, which maps todict
'sget
method of course. Just like dicts, this works for lists/tuples too - in order to return elements based on index. In special cases, this get accessor returns actual proxy methods, such astoString
,valueOf
, etc. Either way, preference is given to Python object first. So only if they do not have these attributes, we return the JS functions. -
Set their attributes. Same as the "get" proxy behavior described above, but instead to set attribute / dict key / list index.
-
There's also this has accessor on proxy objects, which is basically like
in
operator in Python. It checks if attribute/dict key exists in the object.
Methods
Casts a List Python object as JS Array value.
Casts a Bool Python object as JS Boolean value.
Casts a Dict Python object as JS Map value.
Note: from
supports converting both Map and Object to Python Dict.
But this only supports returning a Map.
Casts a Float (Double) Python object as JS Number value.
Casts a Int Python object as JS Number value.
Casts a Set Python object as JS Set object.
Casts a String Python object as JS String value.
Casts a Tuple Python object as JS Array value.
Call the PyObject as a Python function.
Performs an equals operation on the Python object.
Same as maybeGetAttr, but throws an error if the attribute is not found.
Checks if Python object has an attribute of given name.
Calls Python isinstance
function.
Tries to get the attribute, returns undefined otherwise.
Tries to set the attribute, throws an error otherwise.
Returns str
representation of the Python object.
Tries to guess the value of the Python object.
Only primitives are casted as JS value type, otherwise returns a proxy to Python object.
Static Methods
Creates a new Python object from the given JS value.
Only functions are not supported.