PyObject is the base class of all types other than PyCore. Objects of non-builtin classes are instances of PyObject. PyObject implements 4 magic methods to map operations to Python objects.
All class methods, parameters, and return values are defined in the files in the stubs
directory. The documentation does not describe them in detail.
PyObject
: The base class of all other typesPyDict
: Dictionary type, equivalent to PHP associative arraysPyList
: List type, equivalent to PHP indexed arraysPyTuple
: Tuple, an immutable listPyStr
: StringPyModule
: Python package,PyModule
is also a subclass ofPyObject
PyObject -> PyModule
-> PySequenece -> PyList
-> PyTuple
-> PySet
-> PyStr
-> PyDict
-> PyType
Reads the attribute of the Python object.
$pyobj->attr;
The following operations are equivalent:
pyobj.attr
Sets the attribute of the Python object.
$pyobj->attr = 'hello';
The following operations are equivalent:
pyobj.attr = 'hello'
Calls the method of the Python object.
$pyobj->fn($a, $b, $c);
The following operations are equivalent:
pyobj.fn(a, b, c)
Executes a callable object, usually used to execute functions, construct objects.
// Import a py module, name is app.user
$user = PyCore::import('app.user');
// Info is a class
$Info = $user->Info;
// create an object, it is instance of app.user.Info
$info = $Info('Rango', 2023);
The following operations are equivalent:
from app.user import Info
# create an Info object
info = Info('Rango', 2023)
Supports named parameters. Example:
kwargs($a, $b, $c, name: 'hello', world: 'rango');
- Positional parameters must come first, named parameters must come last.
function kwargs($a, $b, $c, $name, $world) {
}
function kwargs(...$kwargs) {
var_dump($kwargs);
}
$kwargs
will contain both the positional parameters and the named parameters, for example in the previous example it will receive:
array(
0 => $a,
1 => $b,
2 => $c,
'name' => 'hello',
'world' => 'rango',
)
function kwargs(...$kwargs) {
kwargs_fn2(...$kwargs);
}
It is possible to forward the named parameters to another function.