0% found this document useful (0 votes)
34 views4 pages

Decoratos

Decorators in python explanation
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views4 pages

Decoratos

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

Decorators in python

Funtion aliasing
-> In python everything is an object.
-> Even function is also internally considered as an object
only.
-> For the existing function, we can give another name,
which is nothing but function aliasing.
-> if we delete one name, still we can access that function
by using alias name.

Nested function
->

A function can return another function


->

We can pass a function as argument to another function?


->

What is a decorator function?


-> A decorator is a function which can take a function as
argument and extend its functionality and returns modified
function with extended functionality.

Def decor(input_func):
def output_func():
.../
return output function
-> The main objective of decorator functions is we can
extend the functionality of existing function without
modifying that function.

-> original function and inner function must take same


number of arguments.

-> if we are calling any function pvm will check if any


decorator is configure for that function, if any decorator is
configure pvm will call that function and pass our function
as an argument to another function.

-> The order is important first we need to define decorator


then the original function.

How to call same function with decorator and without


decorator?
-> decorated_wish = decor(wish)
-> decorated_wish(‘Shoaib’)

Decorator chaining concept?

Deep dive in decorators?


Scopes -> local scope, global, nonlocal, nested
Closures, what are they, closures scopes

Decorators: what are they, how they are related to closures


convenience of using @

Application

Scopes and Namespaces


-> When an object is assigned to a variable a = 10
-> That variable points to some object
-> And we say that the variable (name) is bound to that
object.
-> That object can be accessed using that name in various
parts of our code.
-> But not just anywhere
-> That variable name and it’s binding (name and object)
only ‘exist’ in specific parts of our code.
-> The portion of code where that name/binding is defined,
is called the lexical scope of the variable.
-> These bindings are stored in namespaces.
-> each scope has its namespace.

1) The Global scope


-> The global scope is essentially the module scope.
-> it spans a single file only
-> there is no concept of a truly global (across all the
modules in our entire app) scope in python.
-> The only exception to this are some of the built-in
globally available objects, such as
True, False, None, dict, print,
-> The built-in and global variables can be used anywhere
inside our module. Including inside function

-> Global scopes are nested inside the built-in scope.

2) The local Scope


-> When we create functions, we create variable names
inside those functions using assignments. e.g a = 10
-> Variables defined inside a function are not created until
the function is called.
-> Every time the function is called, a new scope is created.
-> Variables defined inside the function are assigned to that
scope. Function local scope, Local scope
-> The actual object the variable references could be
different each time the function is called.

Nested scopes
->

You might also like