"with canvas:"(Python具有"something()as x:`")在Kivy中是如何隐式工作的? [英] how does `with canvas:` (Python `with something() as x:`) works implicitly in Kivy?

查看:96
本文介绍了"with canvas:"(Python具有"something()as x:`")在Kivy中是如何隐式工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚意识到,至少可以用with Python语句在Kivy中添加顶点指令的方式有些神秘(至少对我而言).例如,with的使用方式如下:

I just realized there is something mysterious (at least for me) in the way you can add vertex instructions in Kivy with the with Python statement. For example, the way with is used goes something like this:

... some code
class MyWidget(Widget)
    ... some code 

    def some_method (self):
        with self.canvas:
           Rectangle(pos=self.pos, size=self.size)

在一开始,我认为这只是我偶尔使用的with Python语句.但是突然我意识到事实并非如此.通常看起来更像这样(示例取自此处):

At the beginning I thought that it was just the with Python statement that I have used occasionally. But suddenly I realize it is not. Usually it looks more like this (example taken from here):

with open('output.txt', 'w') as f:
   f.write('Hi there!')

实例后通常有一个as,对象的别名和别名.在Kivy示例中,我们没有定义和别名仍然可以.但令我感到困惑的部分是,矩形指令仍然与self.canvas相关联.在阅读了with语句后,我坚信Kivy代码应写为:

There is usually an as after the instance and something like and alias to the object. In the Kivy example we don't define and alias which is still ok. But the part that puzzles me is that instruction Rectangle is still associated to the self.canvas. After reading about the with statement, I am quite convinced that the Kivy code should be written like:

class MyWidget(Widget)
    ... some code 

    def some_method (self):
        with self.canvas as c:
           c.add (Rectangle(pos=self.pos, size=self.size))

我假设内部的方法add是被调用的方法.假设基于,我们可以简单地使用self.add (Rectangle(pos=self.pos, size=self.size))

I am assuming that internally the method add is the one being called. The assumption is based that we can simply add the rectangles with self.add (Rectangle(pos=self.pos, size=self.size))

我是否缺少有关with Python语句的信息?还是Kivy实施的某种方式?

Am I missing something about the with Python statement? or is this somehow something Kivy implements?

推荐答案

我不知道Kivy,但我想我可以猜出这种特定构造的工作原理.

I don't know Kivy, but I think I can guess how this specific construction work.

with语句被编程为将其存储在对您隐藏的某些全局变量中,而不是保留要与之交互的对象(画布?)的句柄.然后,在with中使用的语句使用该全局变量来检索对象.在该块的最后,清除全局变量作为清理的一部分.

Instead of keeping a handle to the object you are interacting with (the canvas?), the with statement is programmed to store it in some global variable, hidden to you. Then, the statements you use inside with use that global variable to retrieve the object. At the end of the block, the global variable is cleared as part of cleanup.

结果是权衡取舍:代码不太明确(这在Python中通常是所需的功能).但是,代码较短,可能会导致更容易理解(假设读者知道Kivy的工作原理).这实际上是用Python制作嵌入式DSL的技术之一.

The result is a trade-off: code is less explicit (which is usually a desired feature in Python). However, the code is shorter, which might lead to easier understanding (with the assumption that the reader knows how Kivy works). This is actually one of the techniques of making embedded DSLs in Python.

涉及一些技术.例如,如果您希望能够嵌套这样的构造(将一个with放在另一个内部),而不是简单的全局变量,则需要使用保留一堆此类对象的全局变量.另外,如果需要处理线程,则可以使用线程局部变量而不是全局变量.但是通用机制仍然相同-Kivy使用某种状态,该状态保存在直接控制之外的地方.

There are some technicalities involved. For example, if you want to be able to nest such constructions (put one with inside another), instead of a simple global variable you would want to use a global variable that keeps a stack of such objects. Also, if you need to deal with threading, you would use a thread-local variable instead of a global one. But the generic mechanism is still the same—Kivy uses some state which is kept in a place outside your direct control.

这篇关于"with canvas:"(Python具有"something()as x:`")在Kivy中是如何隐式工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆