0

What does @ mean in python?

I think I've seen it somewhere in a python script, but I don't know what it means. Does anyone here know if it exists? If yes, what does it mean?

1st Aug 2019, 7:43 PM
Paul Grasser
Paul Grasser - avatar
2 Respostas
+ 3
In general means that annotation is used on defined object (class or function). For instance, there is annotation "property" which let's you define custom behavior while getting something. python2: class foo(object): def __init__(self): self._value = 5 @property def value(self): print "getting value.." return self._value f = foo() print f.value above will print: getting value.. 5 There are many different annotations out there and you can define even your own. This is how you create your own annotation: def double_result(f): def wrapper(): result = f() return result*2 return wrapper @double_result def my_super_complex_function(): return 5 print my_super_complex_function() above will print "10" You can also use multiple annotations on one object. @double_result @double_result def bar(): return 3 print bar() above will print 12 (3*2*2)
1st Aug 2019, 7:54 PM
Mateusz R
Mateusz R - avatar
+ 1
Oh cool. Thanks!
1st Aug 2019, 7:58 PM
Paul Grasser
Paul Grasser - avatar