0

I would like to use a decorator for validating data in Python. Normally in Python one uses a decorator on a function, but I would like to use a decorator on a variable, similar to Java:

public class Main {
   public static void main(String[] args) {
      @BoldWrapper
      @ItalicWrapper
      String str = "Hello World";
      // Display <b><i>Hello World</i></b>
   }
}

public @interface BoldWrapper {
    public void wrap() default "<b>" + str + "</b>";
}

public @interface ItalicWrapper {
    public void wrap() default "<i>" + str + "</i>";
}

So in Python I would have something like:

if __name__ == '__main__':
    @BoldWrapper
    @ItalicWrapper
    str = "Hello World";
6
  • 5
    You can only decorate functions/methods and classes Commented Jun 6, 2017 at 9:38
  • You can decorate a function and have it return that string. Commented Jun 6, 2017 at 9:40
  • @fechnert - you can decorate getters/setters which would, from the outside, appear as properties. Commented Jun 6, 2017 at 9:43
  • @zwer - which are functions in the first place ;) but yeah, thats also a possible way Commented Jun 6, 2017 at 9:44
  • Java annotations != Python decorators... see this discussion Commented Jun 6, 2017 at 9:46

1 Answer 1

1

only classes and functions/methods definitions may be decorated (functions/methods since Python 2.4, classes since Python 2.6):

from docs about function definition

A function definition may be wrapped by one or more decorator expressions. Decorator expressions are evaluated when the function is defined, in the scope that contains the function definition.

and class definition

Classes can also be decorated: just like when decorating functions

so

@BoldWrapper
@ItalicWrapper
text = "Hello World"

will cause SynaxError.

There is no need for this feature, because we can just write function calls

text = BoldWrapper(ItalicWrapper("Hello World"))

P. S.

  • do not use names of built-ins like str for your objects,
  • semicolons are redundant in Python

Further reading

Sign up to request clarification or add additional context in comments.

5 Comments

But use lower_case because they are functions and not classes
@fechnert: they may be callable classes, we don't know what is OP's case
Hi, thank you for the answer, but I would really like to know whether in Python is possible to have a decorator on a variable, not alternatives.
@Petronella: i answered that it is impossible because there is no need for that
@Petronella no, it is a syntax error. The decorator syntax is syntactic sugar for f = deco(f) after a function definition. It merely exists for the added benefit of readability - one often wants to see the decorators before reading the function body. Decorators in Python are not the same thing as annotations in Java.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.