5
$\begingroup$

In Python, this code is an example of decorators:

@decorator_func
def main_func():
    # Do stuff

When main_func is called, it will call decorator_func with main_func as the only argument. It will then call the returned function* instead of main_func with the arguments provided.

*A decorator doesn't have to return a function, but that doesn't really matter.

Read this for more info on Python decorators.

Anyway, as you can see above, Python uses @ and then the decorator function name before the main function definition. Are there any other syntax options for implementing this? And what are their advantages and disadvantages?

$\endgroup$

1 Answer 1

3
$\begingroup$

Rust/C# Attributes

Rust and C# use similar syntax for their "attribute" feature, which is closer in functionality to Java's annotations but is used in the same manner as decorators syntactically.

Rust:

#[test]
fn test_foo() {
    /* ... */
}

(Rust also allows attributes to be applied to the whole module, using this syntax:)

#![crate_type = "lib"]

C#:

[Serializable]
public class SampleClass
{
    // ...
}
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .