Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of @ symbols in Python?

Tags:

python

I've noticed in several examples i see things such as this:

# Comments explaining code i think
@innerclass

or:

def foo():
"""
 Basic Doc String
"""
@classmethod

Googling doesn't get me very far, for just a general definition of what this is. Also i cant find anything really in the python documentation.

What do these do?

like image 696
UberJumper Avatar asked Jun 02 '09 13:06

UberJumper


People also ask

What does @function mean in Python?

In Python, a function is a group of related statements that performs a specific task. Functions help break our program into smaller and modular chunks. As our program grows larger and larger, functions make it more organized and manageable. Furthermore, it avoids repetition and makes the code reusable.

What we called symbol in Python?

The % symbol in Python is called the Modulo Operator. It returns the remainder of dividing the left hand operand by right hand operand. It's used to get the remainder of a division problem.

What does the & symbol mean in Python?

Answer. The & symbol is a bitwise AND operator. Used with 1, it basically masks the value to extract the lowest bit, or in other words will tell you if the value is even or odd.

Which symbol is used in Python for processing?

The 'at' (@) symbol is used for class, function and method decorators in python.


2 Answers

They are called decorators. They are functions applied to other functions. Here is a copy of my answer to a similar question.

Python decorators add extra functionality to another function. An italics decorator could be like

def makeitalic(fn):
    def newFunc():
        return "<i>" + fn() + "</i>"
    return newFunc

Note that a function is defined inside a function. What it basically does is replace a function with the newly defined one. For example, I have this class

class foo:
    def bar(self):
        print "hi"
    def foobar(self):
        print "hi again"

Now say, I want both functions to print "---" after and before they are done. I could add a print "---" before and after each print statement. But because I don't like repeating myself, I will make a decorator

def addDashes(fn): # notice it takes a function as an argument
    def newFunction(self): # define a new function
        print "---"
        fn(self) # call the original function
        print "---"
    return newFunction
    # Return the newly defined function - it will "replace" the original

So now I can change my class to

class foo:
    @addDashes
    def bar(self):
        print "hi"

    @addDashes
    def foobar(self):
        print "hi again"

For more on decorators, check http://www.ibm.com/developerworks/linux/library/l-cpdecor.html

like image 160
Abhinav Gupta Avatar answered Oct 06 '22 00:10

Abhinav Gupta


They're decorators.

<shameless plug> I have a blog post on the subject. </shameless plug>

like image 34
Jason Baker Avatar answered Oct 05 '22 23:10

Jason Baker