processing / processing/libprocessing
Python sketches are not Pythonic
还没有人认领这个 Issue。
- 主要语言
- Rust
- 星标
- 69
- 派生
- 14
- 平均合并
- 15 天 22 小时
- 30 天内合并 PR
- 3
描述
Sorry for the vague title, feel free to update it.
Using https://github.com/processing/processing-examples-mewnala/blob/6e416d80852ee9e147b70ae93cbf8f6dda871ba3/Basics/Structure/Redraw/Redraw.py as an example, a mewnala sketch structure and common techniques has several issues that makes it less ideal as Python code:
- Sharing state between frames requires the use of
global - Even when wildcard import is removed, some variables are injected at the runtime and linters catch them as "undefined":
setupfunction is not meaningful, the same results can be achieved just without. Usingsetupto initialize variables etc. causes even more undefined issues because how scopes work in Python.
As an alternative, a class based approach can solve all these issues:
from mewnala import (
Line,
Sketch as BaseSketch,
)
class Sketch(BaseSketch):
def __init__(self, width=640, height=480):
super().__init__()
self.width = width
self.height = height
def setup(self):
self.size = (self.width, self.height)
self.stroke = 255
self.loop = False
self.y = 180
def draw(self):
self.background = 0
self.y -= 4
if self.y < 0:
self.y = self.height
line = Line(0, self.y, self.width, self.y)
line.draw()
def mouse_pressed(self):
self.redraw()
if __name__ == "__main__":
sketch = Sketch(width=1920, height=1080)
sketch.run()
No more magic imports, no more undefined variables, and now the sketch has a Python-native syntax feel much more natural comparing to the previous version.
I removed the setter function calls too, it's a Java-native pattern and feels weird with Python.
We can go one step further and eliminate the setup() What does it do for the sketch can be covered by __init__ in a class context.
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先阅读链接的 Basics/Structure/Redraw/Redraw.py 示例以及引用的 libprocessing issue #150。将当前的 sketch 生命周期与提议的基于类的方法进行比较,然后明确哪些 API 更改属于范围之内,以及更新后的示例将如何展示完成情况。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- developer-experience
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 冷清
- 描述清晰度
- 基本清楚
- 新手友好度
- 38/100