Advance Python Topics

Advance Python Topics

For all the Python programming enthusiasts there, let's look at some of the advanced topics in Python. Before we move forward, one thing to know is that almost everything in Python is an object.

1) Currying in Python:

Currying is a functional design pattern named after the famous Mathematician and founder of the Haskell programming language Haskell Curry. Currying means breaking down a function with multiple arguments into a sequence of single-argument functions.

Example: Let us take a simple sum function that adds three numbers:

def sum(a,b,c):

return a + b + c

sum(2,7,5) -> 14

The same function in curried form is:

def curried_sum(a):

def w(b):

def y(c)

return a + b + c

return y

return w

curried_sum(2)(7)(5) -> 14

Some benefits of currying are easy code reusability, configuration, and low dependency injection.

2) Lambda Functions:

Lambda functions originates from the mathematical branch of Lambda Calculus, invented by Alonzo Church. Lambda functions are short anonymous functions that can have any number of arguments but only a single expression.

In Python, the 'lambda' keyword defines a lambda function. The lambda function returns a functional object. The general syntax is:

lambda arg1, arg2, ... : expression

eg: x = lambda a, b : a * b

print(x(2,4)) -> 8

Some benefits:

Consumes less memory, and evaluation is faster.

3) Meta Classes:

Meta programming means the ability of a program to influence itself. Remember, in Python, everything is an object. Even classes, both in-built and user-defined are objects of a metaclass known as a 'type' class.

eg:

class myClass():

pass

a = myClass()

type(a) -> myClass

type(myClass) -> type

So, MetaClasses create Classes and Classes create objects. To create a custom MetaClass, user user-defined class should inherit the type class and override init() and new(). Whenever we create a new user class, behind the scenes, Python uses the type() class to create it.

The concept of MetaClasses is a bit complex and cannot be summed up in this article.

4) Map function:

The map function returns a map object(an iterator) after applying the given function to each item of an iterable.

eg:

def square(num):

return num * num

my_list = [1,2,3,4,5]

x = map(square, my_list)

print(list(x)) -> [1,4,9,16,25]

We can pass as many iterables to the map function as there are parameters in the function we pass.

5) Closures in Python:

A closure in Python is a nested function that binds the data(variables) of the outer function even when the outer function is no longer alive i.e. not present in memory. A closure binds the data of the outer function to itself.

eg:

def outer_fun():

x = 10 // Here, x is the local variable of the outer_fun

def inner_fun():

print(x + 5)

inner_fun() // Look carefully, we are calling the inner function

outer_fun() // Calling the output function

Output: 15

In the above example, if we would have done something like:

y = outer_fun()

y() --> Throws an error i.e. object type null because the inner function cannot find x.

Closure:

def outer_fun():

x = 10 // Here, x is the local variable of the outer_fun

def inner_fun():

print(x + 5)

inner_fun // Look carefully, we are returning the inner function

y = outer_fun()

y() --> 15 --> This time, the inner function i.e. y bound the value of the outer function i.e. x to itself.

6) Generators:

Generators, as the name suggests, are something that generates some output. In Python, generators are functions that return a generator object that is iterable with a sequence of values. In generators, a function is not evaluated sequentially but in an iterable form.

eg:

def my_generator_func():

print("First value is")

yield 10

print("Second value is")

yield 20

print("Third value is")

yield 30

gen = my_generator_func()

print(next(gen)) -> 10

print(next(gen)) -> 20

print(next(gen)) -> 30

print(next(gen)) -> Error

7) Magic Methods:

Also known as dunder methods, these methods begin and end with double underscore( magicmethodname). In most cases, these methods are implicitly invoked by their respective classes.

eg:

init -> Method to initiate an object of a class.

add -> Method is called when 2 objects are to be combined.

str -> Method is used to create a string object.

Magic methods can be overridden and overloaded as required.

I will discuss some other important advanced Python topics like Multi-Threading and GIL, Operator Overloading, Exception Handling, Regex, Memoization, @staticmethod and @classmethod, Iterators and Iterables, Context Managers, and Decorators in the follow-up post.

Shiwanshu Kumar

Software Engineer at SpotOn

2y

Nice

To view or add a comment, sign in

Others also viewed

Explore content categories