From ca5bf7d138bfe615cff8018c00586e786f5fdc39 Mon Sep 17 00:00:00 2001 From: Yi Xu Date: Wed, 10 Aug 2022 16:00:43 +0800 Subject: [PATCH] [doc] Rename ti.struct_class to ti.dataclass (#5706) --- docs/lang/articles/advanced/odop.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/lang/articles/advanced/odop.md b/docs/lang/articles/advanced/odop.md index 96c9e537bcec7..26a25ddf13e2e 100644 --- a/docs/lang/articles/advanced/odop.md +++ b/docs/lang/articles/advanced/odop.md @@ -278,20 +278,20 @@ print(a.num()) # 6 print(b.num()) # 7 ``` -## Python classes as Taichi struct types +## Taichi dataclasses Taichi provides custom [struct types](../type/type.md#compound-types) for developers to associate pieces of data together. However, it is often convenient to have: 1. A Python representation of the struct type which is more object oriented. 2. Functions associated with a struct type. (C++ style structs) -To achieve these two points, developers can use the `@ti.struct_class` decorator on a Python class. This is heavily inspired by the Python [dataclass](https://docs.python.org/3/library/dataclasses.html) feature, which uses class fields with annotations to create data types. +To achieve these two points, developers can use the `@ti.dataclass` decorator on a Python class. This is heavily inspired by the Python [dataclass](https://docs.python.org/3/library/dataclasses.html) feature, which uses class fields with annotations to create data types. ### Creating a struct from a Python class Here is an example of how we could create a Taichi struct type from a Python class: ```python -@ti.struct_class +@ti.dataclass class Sphere: center: vec3 radius: ti.f32 @@ -301,7 +301,7 @@ This will create the *exact* same type as doing: ```python Sphere = ti.types.struct(center=vec3, radius=ti.f32) ``` -Using the `@ti.struct_class` decorator will convert the annotated fields in the Python class to members in the resulting struct type. In both of the above examples you would create a field of the struct the same way. +Using the `@ti.dataclass` decorator will convert the annotated fields in the Python class to members in the resulting struct type. In both of the above examples you would create a field of the struct the same way. ```python sphere_field = Sphere.field(shape=(n,)) @@ -311,7 +311,7 @@ sphere_field = Sphere.field(shape=(n,)) Python classes can have functions attached to them, as can Taichi struct types. Building from the above example, here is how one would add functions to the struct. ```python -@ti.struct_class +@ti.dataclass class Sphere: center: vec3 radius: ti.f32 @@ -341,9 +341,9 @@ def get_area() -> ti.f32: get_area() # 201.062... ``` -### Notes on struct classes -- Inheritance of struct classes is not implemented. -- While functions attached to a struct with the `@ti.struct_class` decorator is convenient and encouraged, it is actually possible to associate a function to structs with the older method of defining structs. As mentioned above, the two methods for defining a struct type are identical in their output. To do this, use the `__struct_methods` argument with the `ti.types.struct` call: +### Notes +- Inheritance of Taichi dataclasses is not implemented. +- While functions attached to a struct with the `@ti.dataclass` decorator is convenient and encouraged, it is actually possible to associate a function to structs with the older method of defining structs. As mentioned above, the two methods for defining a struct type are identical in their output. To do this, use the `__struct_methods` argument with the `ti.types.struct` call: ```python @ti.func