Skip to content

Commit

Permalink
[Doc] Update dataclass.md (#6876)
Browse files Browse the repository at this point in the history
Issue: #

### Brief Summary

Co-authored-by: Zhao Liang <[email protected]>
Co-authored-by: Olinaaaloompa <[email protected]>
  • Loading branch information
3 people authored Dec 15, 2022
1 parent fea8024 commit 16ac1e2
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions docs/lang/articles/advanced/dataclass.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ sidebar_position: 4

Taichi provides custom [struct types](../type_system/type.md#compound-types) for developers to assemble pieces of data together. However, it would be more convenient to have:

- A Python representation of the struct type which is more object oriented.
- Functions associated with a struct type (C++-style structs).
- A Python representation of the struct type which is more Object-Oriented.
- Functions associated with a struct type.


To achieve the ends, Taichi enabled the `@ti.dataclass` decorator on a Python class. This is inspired by Python's [dataclass](https://docs.python.org/3/library/dataclasses.html) feature, which uses class fields with annotations to create data types.
Expand All @@ -22,19 +22,17 @@ class Sphere:
center: vec3
radius: ti.f32
```
This creates the *exact* same type as using `ti.types.struct()`:
This is the same equivalent as using `ti.types.struct()`:

```python
Sphere = ti.types.struct(center=vec3, radius=ti.f32)
```
The `@ti.dataclass` decorator converts the annotated members in the Python class to members in the resulting struct type. In both of the above examples, you end up with the same struct field.
The `@ti.dataclass` decorator converts the annotated members in the *Python class* to members in the resulting *struct type*. In both of the above examples, you end up with the same struct field.

```python
sphere_field = Sphere.field(shape=(n,))
```

## Associate functions with the struct type
Python classes can have functions attached to them, and so can Taichi struct types. Building from the above example, one can embed functions in the struct as follows:
## Associate Functions with the struct type

Both Python classes and Taichi struct types can have functions attached to them. Building from the above example, one can embed functions in the struct as follows:

```python
@ti.dataclass
Expand All @@ -54,7 +52,7 @@ class Sphere:

Functions associated with structs follow the same scope rules as other functions. In other words, they can be placed in either the Taichi scope or the Python scope. Each instance of the `Sphere` struct type now have the above functions attached to them. The functions can be called in the following way:

```python
```python{3,10}
a_python_struct = Sphere(center=vec3(0.0), radius=1.0)
# calls a python scope function from python
a_python_struct.is_zero_sized() # False
Expand Down

0 comments on commit 16ac1e2

Please sign in to comment.