processing / processing/libprocessing
Python sketches are not Pythonic
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Rust
- Estrellas
- 69
- Forks
- 14
- Merge medio
- 15 d 22 h
- PR fusionados (30 d)
- 3
Descripción
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.
Guía de contribución
No hay ninguna guía de contribución indexada para este repositorio
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza leyendo el ejemplo enlazado Basics/Structure/Redraw/Redraw.py y el libprocessing issue #150 referenciado. Compara el ciclo de vida actual del sketch con el enfoque propuesto basado en clases y, a continuación, aclara qué cambios de API están dentro del alcance y cómo demostraría la finalización un ejemplo actualizado.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- developer-experience
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Tranquilo
- Claridad
- Bastante claro
- Aptitud para principiantes
- 38/100