1///|
2pub trait trait IsPyObject {
obj(Self) -> PyObject
obj_ref(Self) -> @cpython.PyObjectRef
type_name(Self) -> String
}
IsPyObject {
3 (Self) -> PyObject
obj(type parameter Self
Self) -> struct PyObject {
obj: @cpython.PyObjectRef
// private fields
}
PyObject
4 (Self) -> @cpython.PyObjectRef
obj_ref(type parameter Self
Self) -> type @cpython.PyObjectRef
@cpython.PyObjectRef = _
5 (Self) -> String
type_name(type parameter Self
Self) -> String
String = _
6}
7
8///|
9impl trait IsPyObject {
obj(Self) -> PyObject
obj_ref(Self) -> @cpython.PyObjectRef
type_name(Self) -> String
}
IsPyObject with (self : Self) -> @cpython.PyObjectRef
obj_ref(Self
self) {
10 Self
self.(Self) -> PyObject
obj().@cpython.PyObjectRef
obj
11}
12
13///| Return the type name of the object.
14///
15/// ## Example
16///
17/// ```moonbit
18/// test "type_name" {
19/// let t = PyBool::from(true);
20/// inspect!(t.type_name(), content="bool")
21///
22/// let i = PyInteger::from(42);
23/// inspect!(i.type_name(), content="int")
24///
25/// let f = PyFloat::from(3.14);
26/// inspect!(f.type_name(), content="float")
27///
28/// let s = PyString::from("hello");
29/// inspect!(s.type_name(), content="str")
30///
31/// let tup = PyTuple::new(1)
32/// inspect!(tup.type_name(), content="tuple")
33///
34/// let lst = PyList::new()
35/// inspect!(lst.type_name(), content="list")
36/// }
37/// ```
38///
39/// The above code is equivalent to:
40///
41/// ```python
42/// t = True
43/// print(type(t)) # Output: <class 'bool'>
44///
45/// i = 42
46/// print(type(i)) # Output: <class 'int'>
47///
48/// f = 3.14
49/// print(type(f)) # Output: <class 'float'>
50///
51/// s = "hello"
52/// print(type(s)) # Output: <class 'str'>
53///
54/// tup = (1,)
55/// print(type(tup)) # Output: <class 'tuple'>
56///
57/// lst = []
58/// print(type(lst)) # Output: <class 'list'>
59/// ```
60impl trait IsPyObject {
obj(Self) -> PyObject
obj_ref(Self) -> @cpython.PyObjectRef
type_name(Self) -> String
}
IsPyObject with (self : Self) -> String
Return the type name of the object.
Example
test "type_name" {
let t = PyBool::from(true);
inspect!(t.type_name(), content="bool")
let i = PyInteger::from(42);
inspect!(i.type_name(), content="int")
let f = PyFloat::from(3.14);
inspect!(f.type_name(), content="float")
let s = PyString::from("hello");
inspect!(s.type_name(), content="str")
let tup = PyTuple::new(1)
inspect!(tup.type_name(), content="tuple")
let lst = PyList::new()
inspect!(lst.type_name(), content="list")
}
The above code is equivalent to:
t = True
print(type(t)) # Output: <class 'bool'>
i = 42
print(type(i)) # Output: <class 'int'>
f = 3.14
print(type(f)) # Output: <class 'float'>
s = "hello"
print(type(s)) # Output: <class 'str'>
tup = (1,)
print(type(tup)) # Output: <class 'tuple'>
lst = []
print(type(lst)) # Output: <class 'list'>
type_name(Self
self) {
61 Self
self.(Self) -> PyObject
obj().(self : PyObject) -> String
type_name()
62}
63