小编Asw*_*P J的帖子

python中的作用域规则

考虑以下python代码段(我正在运行Python 3)

name = "Sammy" 

def greet():
    name = 'johny'
    def hello():
        print('hello ' + name) # gets 'name' from the enclosing 'greet'
    hello()

greet()
Run Code Online (Sandbox Code Playgroud)

这将产生hello johny预期的输出

然而,

x = 50

def func1():
    x = 20
    def func2():
        print("x is ", x) # Generates error here
        x = 2
        print("Changed the local x to ",x)
    func2()

func1()
print("x is still ",x)
Run Code Online (Sandbox Code Playgroud)

生成一个UnboundLocalError: local variable 'x' referenced before assignment

为什么第一个代码段有效,而第二个代码段无效?

python python-3.x

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

如何实时更新android通知?

我看到一些应用程序实时更新其通知内容。一个例子是倒数计时器应用程序。

在此处输入图片说明

单击+ ADD 1 MIN按钮立即更改通知文本,没有任何延迟。这是怎么做的?或者换句话说,当用户单击按钮时,如何实时更新通知?

在此处输入图片说明

android

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

标签 统计

android ×1

python ×1

python-3.x ×1