processing / processing/libprocessing

Python sketches are not Pythonic

未關閉
#230 2 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 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":
  • setup function is not meaningful, the same results can be achieved just without. Using setup to 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.

貢獻指南

這個儲存庫沒有索引到貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

先閱讀連結的 Basics/Structure/Redraw/Redraw.py 範例以及參照的 libprocessing issue #150。比較目前的 sketch 生命週期與提議的類別式方法,接著釐清哪些 API 變更屬於範圍之內,以及更新後的範例將如何展示完成情況。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
developer-experience
Issue 類型
功能
難度
5/5
預估耗時
一週以上
活躍度
冷清
描述清晰度
基本清楚
新手友好度
38/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。