相关疑难解决方法(0)

范围规则的简短描述?

Python范围规则究竟是什么?

如果我有一些代码:

code1
class Foo:
   code2
   def spam.....
      code3
      for code4..:
       code5
       x()
Run Code Online (Sandbox Code Playgroud)

在哪里x找到?一些可能的选择包括以下列表:

  1. 在封闭的源文件中
  2. 在类命名空间中
  3. 在函数定义中
  4. 在for循环索引变量中
  5. 在for循环中

在执行期间,当函数spam在其他地方传递时,也存在上下文.也许lambda函数的传递方式有点不同?

某处必须有简单的参考或算法.对于中级Python程序员来说,这是一个令人困惑的世界.

python scope dynamic-languages

451
推荐指数
8
解决办法
24万
查看次数

在if语句中初始化变量的范围是什么?

我是Python的新手,所以这可能是一个简单的范围问题.Python文件(模块)中的以下代码让我感到困惑:

if __name__ == '__main__':
    x = 1

print x
Run Code Online (Sandbox Code Playgroud)

在我工作过的其他语言中,这段代码会引发异常,因为x变量是if语句的本地变量,不应该存在于它之外.但是这段代码执行并打印1.任何人都可以解释这种行为吗?模块中创建的所有变量是全局/可用于整个模块吗?

python variables scope if-statement local-variables

247
推荐指数
6
解决办法
10万
查看次数

Python变量范围错误

以下代码在Python 2.5和3.0中按预期工作:

a, b, c = (1, 2, 3)

print(a, b, c)

def test():
    print(a)
    print(b)
    print(c)    # (A)
    #c+=1       # (B)
test()
Run Code Online (Sandbox Code Playgroud)

但是,当我取消注释行(B)时,我得到了UnboundLocalError: 'c' not assigned一行(A).的值ab被正确地打印.这让我感到困惑,原因有两个:

  1. 为什么在行(A)处抛出运行时错误,因为后面的行(B)语句?

  2. 为什么变量ab打印符合预期,同时c引发错误?

我能想到的唯一解释是,赋值创建了一个局部变量,即使在创建局部变量之前,它也优先于"全局"变量.当然,变量在存在之前"窃取"范围是没有意义的.cc+=1c

有人可以解释一下这种行为吗?

python variables scope

195
推荐指数
7
解决办法
6万
查看次数

什么是条件变量初始化的pythonic方式?

由于Python的作用域规则,之后在范围内初始化的所有变量都可用.由于条件不引入新范围,因此不一定需要其他语言的构造(例如在该条件之前初始化变量).例如,我们可能有:

def foo(optionalvar = None):
    # some processing, resulting in...
    message = get_message()
    if optionalvar is not None:
        # some other processing, resulting in...
        message = get_other_message()
    # ... rest of function that uses message
Run Code Online (Sandbox Code Playgroud)

或者,我们可以改为:

def foo(optionalvar = None):
    if optionalvar is None:
        # processing, resulting in...
        message = get_message()
    else:
        # other processing, resulting in...
        message = get_other_message()
    # ... rest of function that uses message
Run Code Online (Sandbox Code Playgroud)

当然,get_messageget_other_message函数可能是多行代码并且基本上是无关的(你可以假设每个路径之后程序的状态是相同的); 这里的目标是message在功能的这一部分之外准备好使用.

我已经看到后一种结构在其他问题中多次使用,例如:

哪种结构更容易接受?

python

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

python函数末尾“ raise”或“ except”以外的“ raise”

这是什么raise做的,如果它不是内部tryexcept条款,而只是作为函数的最后一条语句?

def foo(self):
    try:
        # some code that raises an exception
    except Exception as e:
        pass

    # notice that the "raise" is outside
    raise
Run Code Online (Sandbox Code Playgroud)

此示例打印1,但不打印2,因此必须是last raise语句只是引发了最后抛出的异常。

def foo():
    try:
        raise Exception()
    except Exception as e:
        pass

    print 1
    raise
    print 2

if __name__ == '__main__':
    foo()
Run Code Online (Sandbox Code Playgroud)

有关这种使用方式的任何正式文档?

python python-2.7

4
推荐指数
2
解决办法
4078
查看次数