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?
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.
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.
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.
The 'at' (@) symbol is used for class, function and method decorators in python.
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
They're decorators.
<shameless plug>
I have a blog post on the subject.
</shameless plug>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With