小编JDG*_*JDG的帖子

运行 .dart 文件进行实验。(VS代码)

我正在 Windows 上使用 VS Code 编写一个应用程序,但我喜欢在应用程序中使用之前测试一小段 dart 代码,以确保一切都按我的预期工作。这是一个简单的例子:

lib/
    main.dart
    dart_experiments.dart
Run Code Online (Sandbox Code Playgroud)

并在dart_experiments.dart

void main() {
    print('Hello world.');
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能单独运行这个文件并在控制台中查看结果?

dart

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

带有参数的装饰器:这会是更好的方法吗?

我刚刚开始学习装饰器。我觉得我了解如何修改装饰器以使用参数,但是我不明白为什么要在Python中实现装饰器,因此必须进行此修改。

[编辑:为清楚起见。]对于没有参数的装饰器,以下内容是等效的:

@decorate
def function(arg):
    pass

function = decorate(function)
Run Code Online (Sandbox Code Playgroud)

因此,对于带有参数的装饰器,为什么不让以下内容等效?

@decorate(123)
def function(arg):
    pass

function = decorate(function, 123)
Run Code Online (Sandbox Code Playgroud)

我发现这更加一致,更易于阅读并且更易于编码。

以下是python装饰器的工作方式,以及我希望它们如何工作。[编辑:我还添加了预期的输出。]:

OUTPUT:
function HELLA NESTED! Decorated with OMG!
function NOT AS NESTED! Decorated with NOT SO BAD!

def python_decorator(dec_arg):
    def actual_decorator(func):
        def decorated_func(func_arg):
            print(func.__name__, func(func_arg), "Decorated with", dec_arg)

        return decorated_func
    return actual_decorator

@python_decorator("OMG!")
def function(arg):
    return arg

function("HELLA NESTED!")

def my_expected_decorator(func, dec_arg):
    # I expected the `func` parameter to be like `self` in classes---it's 
    # always first and is given special …
Run Code Online (Sandbox Code Playgroud)

python python-decorators

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

标签 统计

dart ×1

python ×1

python-decorators ×1