小编Pie*_*olo的帖子

关于Lua中"local foo = foo"成语的解释

在Roberta Ierusalimschy的Lua编程(第3版)中说明了这一点

Lua的一个常见习语是

local foo = foo

此代码创建一个局部变量,foo并使用全局变量的值对其进行初始化foo.(foo只有在声明后,本地变得可见.)当块需要保留原始值时,foo即使稍后某些其他函数改变了全局值,这个习惯用法也很有用foo; 它还加快了访问速度foo.

有人可以更详细地解释这个并提供一个简单的例子吗?

目前,我能想到的唯一用途就是管理与全局变量具有相同名称的局部变量(在给定的块中),以便在块之后保持全局变量不变.

一个例子:

foo = 10
do
   local foo = foo
   foo = math.log10(foo)
   print(foo)
end
print(foo)
Run Code Online (Sandbox Code Playgroud)

这给了:

1
10
Run Code Online (Sandbox Code Playgroud)

但是完全可以在不使用成语的情况下完成:

bar = 10
do
   local bar = math.log10(bar)
   print(bar)
end
print(bar)
Run Code Online (Sandbox Code Playgroud)

这给出了相同的结果.所以我的解释似乎并不成立.

lua

5
推荐指数
1
解决办法
361
查看次数

在 Python 3 中的一行上打印一个序列

我已经设法使排序正确,但是我不确定如何将它打印在同一行上。我有这个:

n = input ("Enter the start number: ")
i = n+7

if n>-6 and n<93:
    while (i > n):
        print n
        n = n+1
Run Code Online (Sandbox Code Playgroud)

并尝试过这个:

n = input ("Enter the start number: ")
i = n+7

if n>-6 and n<93:
    while (i > n):
        print (n, end=" ")
        n = n+1
Run Code Online (Sandbox Code Playgroud)

python printing sequence python-3.x

3
推荐指数
1
解决办法
8905
查看次数

为什么Lua将分区截断为第一位?

我写了一个小脚本(我在Lua的一个非常基本的级别),它输出一个值表:

-- ProbabilityOfOralExam.lua
-- This script outputs a data file where in the first column are
-- written the numbers that are the possible sum of the digits for
-- the numbers belonging to Range and in the second column is
-- computed how many times that number is repeated in Range.
--
-- $ lua ProbabilityOfOralExam.lua RANGE OUTFILE
-- (OUTFILE = outprob.txt)
--
-- If RANGE is not given, default is 100.
-- If OUTFILE is not given, standard …
Run Code Online (Sandbox Code Playgroud)

debugging lua

1
推荐指数
1
解决办法
146
查看次数

Tkinter的.after()和递归

要及时更新小部件,我使用该.after()方法,通常采用以下形式:

def update():
    do_something()
    <widget>.after(<delay>, update)
Run Code Online (Sandbox Code Playgroud)

我的理解是,小部件等待一定的时间然后执行该update()功能,最后小部件在重新执行该功能之前再次等待,等等.

这在我看来很像递归.所以,问题是:.after()实际上是否通过递归方式工作?

如果确实如此,则递归深度存在限制,但以下示例应证明永远不会达到此限制:

from tkinter import *

counter = 0

def count():
    global counter
    counter += 1
    lbl.config(text=counter)
    root.after(10, count)

root = Tk()
lbl = Label(root, text='0')
lbl.pack()
Button(root, text='Start count', command=count).pack()
root.mainloop()
Run Code Online (Sandbox Code Playgroud)

在我的系统中,递归深度的限制是1000,但是这个例子在几秒钟内远远超过了该值,直到我停止它为止.

python recursion tkinter python-3.x

1
推荐指数
2
解决办法
1629
查看次数

标签 统计

lua ×2

python ×2

python-3.x ×2

debugging ×1

printing ×1

recursion ×1

sequence ×1

tkinter ×1