processing / processing/libprocessing

Python sketches are not Pythonic

Aperta
#230 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
Rust
Stelle
69
Fork
14
Merge medio
15g 22h
PR unite (30g)
3

Descrizione

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.

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia leggendo l’esempio collegato Basics/Structure/Redraw/Redraw.py e il libprocessing issue #150 indicato. Confronta il ciclo di vita attuale dello sketch con l’approccio proposto basato sulle classi, quindi chiarisci quali modifiche all’API rientrano nell’ambito e come un esempio aggiornato dimostrerebbe il completamento.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
developer-experience
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.