Decorators in Python

Decorators are simply functions in python that add extra functionality to the current function without changing the structure of the current function.Suppose we have function that we already written to get user name and print it.But later the requirement changes the display should be always in small letter in that case we can use Decorators in Python.Decorators in Python can be used to fulfill this by writing a Decorators functions to accomplish this task.

Decorators in Python example

def display_msg():
   return 'Welcome Back '

display_msg()  

OutPut : Welcome Back 



def decorator_lowercase(function):
   def wrapper():
       func = function()
       string_lowercase = func.lower()
       return string_lowercase
   return wrapper

@decorator_lowercase
def display_msg():
   return 'Welcome Back '

display_msg()  


OutPut : welcome back 

Python Namespaces and Scope

Python Namespaces is a way that make sure that object names are unique and can be used without any conflict.Python maintain namespace in the form of dictionary with ‘name as key’ mapped to a corresponding ‘object as value’.

Realtime example for namespace is their may be multiple employee with same name John ,but if we specifically identify John emp1009 their will only one person.The same way based on the namespace Python interpreter understands what exact method is trying to point to in the code.

Types of namespaces in Python

Local Namespace : Local Names space is created inside a function and gets cleared when the function returns.so it is temporary created inside the functions when it is called and cleared when the function returns

Global Namespace : Global Namespace are the namespace from imported module or package .The namespace are created when the package is imported in the script and it will be last long until until the execution of the script.

Built-in Namespace : includes built-in functions and the built-in names for various types of exceptions.

Lifecycle of a Namespaces purely depend on the scope of the object the mapped are mapped to it.

Python Namespaces

What is __init__ in python

__init__ in python is like constructor in other object oriented languages like PHP or Java, it automatically executed whenever an object or instance of that class is created.

See __init__ in python example

# class Employee definition
class Employee:
def __init__(self, fname, lname, age, department):
self.firstname = fname
self.lastname = lname
self.age = age
self.department= department

def attendance(employee_id, date):
self.attandante_date= date

# creating a new object or instance of Employee

stu1 = Employee(“Joji”, “John”, 32, “Marketing”)

Modules and Packages in Python

Modules and Packages are the most important and widely used concept in python.Modules and Packages support reusability and easy coding for programmers.

What is a Modules in Python

A Modules is simply a single python file with extension .py. Modules contains global variables and group of functions .Modules is a single executable .py files.we can use functions and global variables in module in any python page by simply importing that module in that page.Here is the example usage of a Modules

Module calculator.py

def sum(a,b):
    sum = a + b 
    print("The sum is: ", sum)


Importing and calling that Module

import calculator

calculator.sum(3,4)

Modules and Packages in Python

What is a Packages in Python

Packages is a collections of modules, it simple a directory that contains lots of modules and a __init__.py file.The package may contains sub package .

employee(Package)
| __init__.py (Constructor)
| contact.py (Module)
| department.py (Module)
| salary.py (Module)