2

A code that I'm reading uses @batch_transform. What does the @ symbol do? Is it ipython specific?

from zipline.transforms import batch_transform
from scipy import stats

@batch_transform
def regression_transform(data):
    pep_price = data.price['PEP']
    ko_price = data.price['KO']
    slope, intercept, _, _, _ = stats.linregress(pep_price, ko_price)

    return intercept, slope
Nyxynyx
  • 61,411
  • 155
  • 482
  • 830

3 Answers3

2

The @ syntax signals that batch_transform is a Python decorator, read more about it in the wiki, quoting:

A Python decorator is a specific change to the Python syntax that allows us to more conveniently alter functions and methods (and possibly classes in a future version). This supports more readable applications of the DecoratorPattern but also other uses as well

Also take a look at the documentation:

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. The result must be a callable, which is invoked with the function object as the only argument. The returned value is bound to the function name instead of the function object. Multiple decorators are applied in nested fashion

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • 7
    Epic answer on stackoverflow: http://stackoverflow.com/questions/739654/how-can-i-make-a-chain-of-function-decorators-in-python – nickzam Nov 07 '13 at 18:51
1

It is a decorator. A Python decorator.

Function, method, or class definitions may be preceded by a @ special symbol known as a decorator, the purpose of which is to modify the behavior of the definition that follows.

Decorators are denoted with the @ symbol and must be placed on a separate line immediately before the corresponding function, method, or class. Here’s an example:

class Foo(object):
    @staticmethod
    def bar():
        pass

Also, you can have multiple decorators:

@span
@foo
def bar():
    pass

Here is a good lesson on it. Here is a great thread on it for SO.

Community
  • 1
  • 1
Games Brainiac
  • 80,178
  • 33
  • 141
  • 199
0

any function can be wrapped using a decorator by @ symbol

example

def decor(fun):
    def wrapper():
        print "Before function call"
        print fun()
        print "Before function call"
    return wrapper

@decor
def my_function():
    return "Inside Function"

my_function()


## output ##
Before function call
Inside Function
Before function call

[NOTE] even classmethod and staticmethod's are implemented using decorators in python

In your case, there would be function called batch_transform, and you have imported it !

Siva Cn
  • 929
  • 4
  • 10