The bellow code sample is from a BeeWare suite example. (https://github.com/eliasdorneles/drawingapp-voc/blob/master/drawingapp/app.py)
What does the expression implements=android.view.View[OnClickListener]
means? There should be (a list of) base class(es). Is it some incompatible special syntax of the framework or standard python I can not comprehend (and missing from the documentation)?
Moreover, we are using OnClickListener
here which is never imported. There is no wildcard imports (*) and no module prefix before the symbol like android.Constants.OnClickListener
or someting like that. How can the Python interpreter find the OnClickListener
value in this case?
import android
from android.widget import LinearLayout, TextView, Button
import android.content.Context
from android.graphics import Bitmap, Canvas, Color, Paint, Path, PorterDuff
from android.view import MotionEvent, Gravity
import android.view
class ButtonClick(implements=android.view.View[OnClickListener]):
def __init__(self, callback, *args, **kwargs):
self.callback = callback
'''
So, the key to the mistery here is that this is not quite Python code- rather, it is a source file meant to be transpiled with VOC - that will generate Java bytecode, which will be further processed to work as an Android application using the standard Android Api.
As a transpiler, VOC relies on syntax which are valid Python, but it allows itself to take detours from the official language - like using keyword arguments on class definitions. That would only make sense in Python if it were inheriting a class which would implement the special method __init_subclass__
, or use a custom metaclass to interpret these arguments - otherwise they'd have no effect. Also, VOC probably use these keywords (extends
and implements
) to indicate exactly these words as they are in Java class declaration syntax.
Also, as you put it, the name OnClickListener
as it is in there would fail with a NameError
in normal Python - it is likely that VOC defines additional names that behave like builtin names. In ordinary Python, if you create a function/method definition instead of a class definition, it is possible to makeuse of non-imported or undefined names as annotations. That is android.view.View[OnClickListener]
would follow :
instead of =
, in a function definition. Annotations are lazily evaluated in Python 3.7 (but not in Python 3.6), so it would not cause an error. As it is, though, it would just raise a NameError in normal Python.
Here is a quick start for Python android apps using VOC in PyBee https://pybee.org/project/using/android-app/
update answer extensively reworded after I found out about VOC, though the first findings on "this is not Python" were correct)