Pythonic Toolkit

Sweta Barnwal
6 min readDec 30, 2020

Get to know more …

Image source: Author

In this blog, I try to scratch Python’s inner working, queries, and some tricks:

  1. Is Python interpreted or compiled? Are there ways to compile the code?
  2. Which is faster in python — Searching in a list or a dictionary. And why?
  3. How can we get the code of a python object?
  4. What are the ways to make python code work faster?
  5. What is exec function and how we can use it?
  6. What are metaclasses and dataclasses in python?
  7. what is __call__ method in python?

Is Python interpreted or compiled? Are there ways to compile the code?

According to most of the Python programming books and pythonic, Python language is interpreted. But there lies a hidden compilation step. Python code is first compiled into bytecode. Bytecode is a low-level set of instructions that are executed by an interpreter. These bytecode instructions internally get converted into a python virtual machine(p.v.m). Python bytecode can be executed on any platform (Windows, macOS, Linux, etc) as long as they have the same version.

Python, being an interpreted language, is accused to slow. They are slower because their method, object, and global variable space model are dynamic. Above all, the most affecting reason for being slow is because the interpreter has to do extra work to have the bytecode instruction translated into a form that can be executed on the machine.

Which is faster in python — Searching in a list or a dictionary. And why?

Looking out items in a Python dictionary is much faster than looking up items in a Python list. Dictionaries are Python’s built-in mapping type and so have also been highly optimized. In cases when it does not have a key-value pair then it utilizes a data structure, hashmap.

Lists are accessed by index number, searching for an item in a list can be very slow as the only way to search a list is to access each item in the list starting from the zeroth element and going up to the last element in the list.

How can we get the code of a python object?

We all know that Objects are an encapsulation of variables and functions into a single entity and in other words, it is an instance of the class. Whatever is visible to us in Python is considered to be an object. But sometimes it's really good to think and do things out of the box. What say ?? Isn’t it true?

To retrieve what our functions source code looks like, Python has a built-in standard library known as inspect module. It provides the introspection of live objects, classes, and the source code of the same. The inspect module provides the user to make use of functions/methods in it to fetch the source code for the same, extract and parse the needed library documentation.

import inspectclass MyClass:
""" A class """
def __init__(self):
pass
def fun1(self, a, b):
return 0
def fun2(self):
return 1
print(inspect.getsource(MyClass))

Output:

class Myclass:
""" A class """
def __init__(self):
pass
def fun1(self, a, b):
return 0
def fun2(self):
return 1

We import the inspect module and retrieve the source code. The source code is returned as a single string. An IOError is raised if the source code cannot be retrieved.

What are the ways to make python code work faster?

Found some amazing tricks to fasten your python code. I basically come from a traditional language world and expected that Python would be even faster but bad luck !!! It's a programming language developed just to give a twist to the programmer, making their way to coding a bit easier than ever before.

Image source: Author

Exactly this is how I felt while doing my project. Now since I have done some research on it, curious to share what I have learned.

Python is a boon to programmers as it is dynamically typed. It comes up with many built-in-functions implemented in c. Thus boosting up its efficiency. Get to know the usage of different functions and increase your familiarity. This will reduce our redundancies.

#examples
sum(),abs(),min(),zip(),len(),enumerate(),etc
  • Check from the set and not from the list. Traversing a list has always been a tedious job. According to my personal experience and some sort of research, convert the list into a set and then continue your traversing. This will increase its efficiency. Howsoever, iterating through the set is not faster than iterating through a list.
  • Save your computer memory by exporting all the unused, unnecessary, and irrelevant libraries. When you end up importing everything at the top, it won't take extra time. Import the libraries within the corresponding function.
  • Avoid string concatenation with the + operation. In place, use join().
  • use zip() for packaging, unpacking multiple iterables.
  • set() remove duplicates from the list. In this way, the number of elements is reduced. Then use in, which is a very fast operation on sets. Thus, combining set() and in improves efficiency.
  • — eq — is a special function for comparing values on both sides.
  • List comprehension
  • Use map function.

What is exec function and how we can use it?

Python has a built-in function named exec() which is present in the math library. It is used to dynamically execute the python generated object code or strings. Python exec() function doesn’t return anything.

Syntax of exec() :exec(source [, globals [, locals]])

Example :

code = "print("Hello World!")"
exec(somecode)
#output
Hello World!

exec() is a useful python function, has a specific use-case approach, and shouldn’t allow any untrusted code to be executed using exec() as it can really harm your system.

What are metaclasses and dataclasses in python?

Image source: Author

A metaclass is a class whose instances are classes. Like an “ordinary” class defines the behavior of the instances of the class, a metaclass defines the behavior of classes and their instances. They aren’t supported by every object-oriented programming language. Python provides you a way to get under the hood and define custom metaclasses. Every type in Python is defined by the class. Unlike C or Java where int, char, float are primary data types, in Python, they are objects of int class or str class. So we can make a new type by creating a class of that type.

num = 100
print("Type of num is:", type(num))#Output: Type of num is: <class 'int'>

Dataclass module is introduced in Python 3.7 as a utility tool to make structured classes especially for storing data. They are just regular classes that are geared towards storing state, more than containing a lot of logic. Every time you create a class that mostly consists of attributes you made a data class.The DataClasses are implemented by using decorators with classes. The dataclass provides an in built __init__() constructor to classes which handle the data and object creation for them.

What is __call__ method in python?

__call__() is a built-in method in python. The — call — method in the meta-class allows the class's instance to be called as a function, not always modifying the instance itself. It is called when the instance is called.

class Add:
def __init__(self):
print("Instance Created")
def __call__(self, a, b):
print("Sum:",sum(a,b))
ans = Add()
ans(10, 20)
Output:
Instance Created
Sum:30

Hope, after reading this article you must have gained something.

Thank you :)

--

--