3

次のコード サンプルは、BeeWare スイートの例からのものです。( https://github.com/eliasdorneles/drawingapp-voc/blob/master/drawingapp/app.py )

表現のimplements=android.view.View[OnClickListener]意味は?基本クラス (のリスト) があるはずです。フレームワークまたは標準のpythonの互換性のない特別な構文が理解できない(そしてドキュメントにない)のですか?

また、OnClickListener輸入されたことのないこちらを使用しています。ワイルドカード インポート (*) はなく、シンボルの前にモジュール プレフィックスもありませんandroid.Constants.OnClickListener。この場合、Python インタープリターはどのようにOnClickListener値を見つけることができますか?

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

'''

4

2 に答える 2

3

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)

于 2019-01-07T22:12:44.413 に答える