小编zra*_*ram的帖子

dart 支持非局部变量吗?

例如,在 Python 中我们有一个非局部特征:

nonlocal 关键字用于处理嵌套函数内的变量,其中变量不应属于内部函数。使用关键字nonlocal 来声明该变量不是局部的。

nonlocal 语句声明,每当我们更改名称 var 的绑定时,绑定都会在已绑定 var 的第一帧中更改。回想一下,如果没有非本地语句,赋值语句将始终在当前环境的第一帧中绑定名称。nonlocal 语句指示名称出现在环境中除第一个(局部)帧或最后一个(全局)帧之外的某个位置。

Dart中有类似的东西吗?

以下是来自 Python 的代码示例:

def make_withdraw(balance):
    """Return a withdraw function that draws down balance with each call."""
    def withdraw(amount):
        nonlocal balance                 # Declare the name "balance" nonlocal
        if amount > balance:
            return 'Insufficient funds'
        balance = balance - amount       # Re-bind the existing balance name
        return balance
    return withdraw
Run Code Online (Sandbox Code Playgroud)

我无法使用的 Dart 伪翻译nonlocal

makeWithdraw(balance) {
  //Return a withdraw function that draws down balance with each call. …
Run Code Online (Sandbox Code Playgroud)

python variables scope dart

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

标签 统计

dart ×1

python ×1

scope ×1

variables ×1