This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# PyTraits
## Introduction
PyTraits is a Python library that provides functionality similar to Rust traits. It allows you to define protocols using `impl` or `trait` annotations to specify the behavior that classes should implement.
## Installation
You can install PyTraits using pip:
```bash
pip install pytraits
```
## Usage
使用 PyTraits,首先需要使用 `trait` 注解定义一个协议:
```python
fromtypingimportruntime_checkable
@runtime_checkable
classPrintable:
defprint(self):
pass
```
然后,可以使用`impl`或`trait`装饰器为已定义的类实现该协议:
```python
frompytraitsimportimpl,trait
classMyClass1:pass
@impl(Printable)
classMyClassImplPrintable(MyClass1):
defprint(self):
print("从 MyClass 打印")
classMyClass2:pass
@trait
classMyClassTraitPrintable(MyClass2,Printable):
defprint(self):
print("从 MyClass2 打印")
mc1:MyClass1|Printable=MyClass1()
mc2:MyClass2|Printable=MyClass2()
mc1.print()
mc2.print()
```
现在,`MyClass` 实现了 `Printable` 协议,可以相应地使用。
## Contributing
Contributions are welcome! If you find any issues or have suggestions for improvement, please open an issue or submit a pull request on GitHub.
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.