From f36fa55d6390414a238431b5e1cca7276008763e Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 5 May 2024 18:38:59 +0800 Subject: [PATCH] Update cheatsheet.md --- docs/cheatsheet.md | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/docs/cheatsheet.md b/docs/cheatsheet.md index cb0f8347..28ace536 100644 --- a/docs/cheatsheet.md +++ b/docs/cheatsheet.md @@ -115,11 +115,11 @@ Dict& g = py_cast(vm, obj); // reference cast Get native types without type checking ```cpp -// unsafe version 1 (for int and float you must use `_py_cast`) +// unsafe version 1 (for int object you must use `_py_cast`) i64 a = _py_cast(vm, obj); f64 b = _py_cast(vm, obj); Tuple& c = _py_cast(vm, obj); -// unsafe version 2 (for others, you can use both versions) +// unsafe version 2 (for others, you can also use `PK_OBJ_GET` macro) Str& a_ = PK_OBJ_GET(Str, obj); List& b_ = PK_OBJ_GET(List, obj); Tuple& c_ = PK_OBJ_GET(Tuple, obj); @@ -130,14 +130,14 @@ Tuple& c_ = PK_OBJ_GET(Tuple, obj); Access built-in python types ```cpp -PyObject* int_t = vm->_t(vm->tp_int); -PyObject* float_t = vm->_t(vm->tp_float); -PyObject* object_t = vm->_t(vm->tp_object); -PyObject* tuple_t = vm->_t(vm->tp_tuple); -PyObject* list_t = vm->_t(vm->tp_list); +PyObject* int_t = vm->_t(VM::tp_int); +PyObject* float_t = vm->_t(VM::tp_float); +PyObject* object_t = vm->_t(VM::tp_object); +PyObject* tuple_t = vm->_t(VM::tp_tuple); +PyObject* list_t = vm->_t(VM::tp_list); ``` -Access extended python types +Access user registered types ```cpp Type voidp_t = vm->_tp_user(); @@ -147,7 +147,7 @@ Check if an object is a python type ```cpp PyObject* obj; -bool ok = is_type(obj, vm->tp_int); // check if obj is an int +bool ok = is_type(obj, VM::tp_int); // check if obj is an int ``` Get the type of a python object @@ -155,6 +155,7 @@ Get the type of a python object ```cpp PyObject* obj = py_var(vm, 1); PyObject* t = vm->_t(obj); // +Type type = vm->_tp(obj); // VM::tp_int ``` Convert a type object into a type index @@ -237,21 +238,21 @@ Convert a python object to string ```cpp PyObject* obj = py_var(vm, 123); -Str s = vm->py_str(obj); // 123 +std::cout << vm->py_str(obj); // 123 ``` Get the string representation of a python object ```cpp PyObject* obj = py_var(vm, "123"); -Str s = vm->py_repr(obj); // "'123'" +std::cout << vm->py_repr(obj); // "'123'" ``` Get the JSON representation of a python object ```cpp PyObject* obj = py_var(vm, 123); -Str s = vm->py_json(obj); // "123" +std::cout << vm->py_json(obj); // "123" ``` Get the hash value of a python object @@ -330,4 +331,3 @@ Create a native module PyObject* mod = vm->new_module("test"); vm->setattr(mod, "pi", py_var(vm, 3.14)); ``` -