Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Decorators

Sahar Nasiri
Sahar Nasiri
7,454 Points

Needs of decorator

What is different between these two codes?

def sub(x, y):
        return x - y
>>> sub(5, 2)

AND

>>> @logme
def sub(x, y):
        return x - y
>>> sub(5, 2)

Why do I need to use decorator?!

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

The need to use a decorator such as logme depends on what functionality is inside of logme. It might be that logme silently writes data to a logfile that is available for later inspection.

So the base answer is the difference between the two codes is the decorated one will perform extra work done in logme in addition to what is defined in the function sub.

Michal Janek
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Michal Janek
Front End Web Development Techdegree Graduate 30,654 Points

So the base answer is the difference between the two codes is the decorated one will perform extra work done in logme in addition to what is defined in the function sub.

This made it so much clearer.