Python 中的块作用域 [英] Block scope in Python

查看:55
本文介绍了Python 中的块作用域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您使用其他语言编写代码时,有时会创建一个块作用域,如下所示:

When you code in other languages, you will sometimes create a block scope, like this:

statement
...
statement
{
    statement
    ...
    statement
}
statement
...
statement

一个目的(许多目的)是提高代码可读性:显示某些语句形成一个逻辑单元或某些局部变量仅在该块中使用.

One purpose (of many) is to improve code readability: to show that certain statements form a logical unit or that certain local variables are used only in that block.

在 Python 中是否有一种惯用的方式来做同样的事情?

Is there an idiomatic way of doing the same thing in Python?

推荐答案

不,没有用于创建块作用域的语言支持.

No, there is no language support for creating block scope.

以下构造创建作用域:

  • 模块
  • 班级
  • 函数(包括 lambda)
  • 生成器表达式
  • 理解(字典、集合、列表(在 Python 3.x 中))

这篇关于Python 中的块作用域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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