PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

Python · Theory

Decorators in Python

← All stacks

Theory

85/268

Decorators in Python

A decorator wraps a function to add behaviour without rewriting its core. @my_decorator above a def is sugar for f = deco(f). Uses: log calls, time a function, check login, cache. Under the hood it is a function that takes a function and returns a new one.

Don’t start with functools.wraps on day one unless they ask. Explain f = deco(f), then one use: timing print. Keep the wrapped function’s job the same.

Decorators in Python — output — HI ASHA. @shout wraps greet and uppercases the result.

Exam tip

f = deco(f) mental model.

Example

# Decorator
def shout(fn):
    def wrap(x):
        return fn(x).upper()
    return wrap

@shout
def greet(name):
    return "hi " + name

print(greet("Asha"))

Decorators in Python — output: HI ASHA. @shout wraps greet and uppercases the result.

Short notes

  • Def@decorator wraps a function.
  • Rulef = deco(f).
  • Uselog / time / auth.

Questions

1

Explain Decorators as if you are teaching a junior — definition, then one tiny script.

2

What does the example print, and why?

3

What mistake do freshers make with Decorators?

Previous← TimsortNextGenerators in Python →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.