-1

I recently came across someone using the @ function in a Python program.

@Square

But I can't seem to find out anywhere online what the function does.

2 Answers2

1

If by "@ function" you mean decorator, its probably a function/decorator they created, if you saw this from a video, please link it

Scatt
  • 41
  • 4
1

This is a method decorator, and probably looks something like this:

@cache
def my_function(a,b,c):
    ...

In short its a way you can wrap a function with some other functionality. For example when using the cache decorator the wrapper will create a cache key and store the output of the function. The next time you call the function it will look up the keys first and save time doing compute.

Its a good way to short cut adding code which would otherwise be run before and after many functions.

In the instance of cache without a decorator you would do something like:

def check_cache():
    ...

def save_cache():
    ...

def my_function1(a,b,c):
    check_cache(a,b,c)
    ...
    save_cache()

def my_function2(a,b,c):
    check_cache(a,b,c)
    ...
    save_cache()

I would recommend checking out the following resource which will help explain their usage a lot better than I can.

Matt Seymour
  • 8,880
  • 7
  • 60
  • 101