0% found this document useful (0 votes)
3 views

Minor Project

Uploaded by

nisarghalpati81
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Minor Project

Uploaded by

nisarghalpati81
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

What is a Decorator…?

A decorator is a method or function that alters how the other


function behaves.
They are typically used for logging, access control,
instrumentation, caching, etc.

How Decorators Work


Treat Functions as First-Class People: Functions can be arguments
to other functions, returns from functions, and variables in Python.

Defining a Decorator:
A decorator is a function that accepts an argument (usually another
function).
Within the decorator function you can define a wrapper function
which performs before or after the original function.

Basic Syntax of a Decorator


def my_decorator(func):
def wrapper():
# String to print at the beginning of the function.
func() # Execute the original function.
# Methods to call after the function.
return wrapper

Common Use Cases


Logging: To log function calls.
Authorization: To determine whether or not a user is authorized to
use a function.
Caching: Save a result from a high cost function call and print the
cached result whenever they have to enter the same value.

Built-in Decorators
Python has some built-in decorators like:
@staticmethod : A method which does not read any of the class's
properties.
@classmethod : Specifies a method that takes a class instead of an
instance as its first argument.
@property : Declare a method as a class property.

Important Considerations
Decorators can be a bit challenging to debug since they
overwrite the original function. Use functools.wraps to store the
original function’s metadata.

Example:
Now as you can see, Here I have created a simple program using
DECORATOR. In this program I have used “intro” as a wrapper and
greeting as a function.
When we will run this program, as a output first we will get “Hi,
my self N “. Then it will print “I am a python developer” and then it
will print “What’s your name…? “ , then it will take name as a input
from user and will store name in variable a and at the end it will print
“It’s nice to meet you ‘input_name’”.

What are python Generators….?


Generators are a unique type of iterator in Python that enable
you to traverse a sequence of values. Unlike lists, they don't keep all
values in memory, which makes them more memory efficient.

Creation of Python Generators:


Generators are made using the yield keyword within a function.
Each time the function is invoked, it can yield a value and pause its
execution.

Using Generators:
You can loop through a generator or use functions like next() to get
values one at a time.

Benefits of Generators:
Memory Efficiency: Generators do not hold the entire sequence in
memory. They produce each value as needed.
Lazy Evaluation: Values are generated only when requested, which
can enhance performance, especially with large datasets.

Use:
Generators are ideal for reading large files line by line, creating
infinite sequences, or streaming data.

Example:
Here I have defined a simple generator. In this generator I have
taken three values.
Then I have stored this generator in variable “gen”.
Then when we use next(gen), compiler will print first Initial value,
Then again when we will use next(gen), compiler will print Middle
value, and third time it will print Last value.
Conclusion:
Python decorator and generator can be used in various way in python
programming, and they are easy to study and apply.

You might also like