Python Basics, Demystified

Jyotiprasad Patil
4 min readDec 30, 2020
Source : Medium

Hello, Everyone. I am back to writing blogs after a long time and this time it is a bit different as the topic for the blog is the most popular computer language, Python.

In this blog I would love to demystify or better say answer some of the basic questions associated with python which no matter whether you are a beginner or have some experience in python may get.

The first question in this is

  1. Is python interpreted or compiled? Are there ways to compile the code?

If we take a look at the definition of Python which is defined as “Python is an interpreted, high-level, general-purpose programming language. It is is dynamically typed and garbage-collected.” It pretty much sums up the question but there still remains more to it as python is an interpreted language but compilation is also a step. Instead of translating source code to machine code, it is translated to byte code. If we want to compile the python code then we can use Cython which generates C code which can then be compiled by any C compiler.

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

The answer for this question is that Python Dictionaries are much more faster that looking up items in a python list but this comes with its own cons that dictionaries use a lot of memory. Looking up in dictionaries is faster because Python implements dictionaries using hash tables which is in turn because dictionaries are python’s built in mapping type and so have also been highly optimized.

3. How can we get the code of a python object?

To get the code of a python object we use the getsource() method of inspect module to get the source code of the function.

inspect.getsource(object) should do the job for us.

4. What are the ways to make python code work faster?

Here are some of the ways to make python code run faster.

a) Avoiding Unwanted Loops

We can try using switch instead of if-else

b) Trying out multiple coding approaches

This basically means thinking out of the box.

c) Keeping the code small and clean.

I know that this does not seem as a solution but it always is :)

5. What is exec function and how we can use it?

So this is basically a built in function which can be found in the math library which is used to dynamically execute the code of python programs. The code can be passed is as a string or object code to this function.

exec(object, globals, locals)This is the syntax for the exec function

where,

objects are string or code object passed onto the method, globals are a dictionary of available global methods and variables and locals are a dictionary of available local methods and variables.

6. What are metaclasses and dataclasses in python?

A metaclass is a class of class. A class defines how an instance of the class behave while a meta class defines how a class behaves. A class can be said as an instance of the meta class. While we can use arbitrary callables for metaclasses but the better approach is to make it an actual class itself. An usual example of metaclass is the type

Dataclass is basically a module which provides a decorator and functions for automatically adding generated special methods such as __init__() and __repr__() to user-defined classes.

An Example -

from dataclasses import dataclass

@dataclass
class InventoryItem:
"""Class for keeping track of an item in inventory."""
name: str
unit_price: float
quantity_on_hand: int = 0

def total_cost(self) -> float:
return self.unit_price * self.quantity_on_hand

7. What is __call__ method in python?
Python has a set of built-in methods and __call__ is one of them. The __call__method enables Python programmers to write classes where the instances behave like functions and can be called like a function.

An Example -

class Product:

def __init__(self):

print("Instance Created")

# Defining __call__ method

def __call__(self, a, b):

print(a * b)

# Instance created

ans = Product()

# __call__ method will be called

ans(10, 20)

Output -

Instance created

200

Thanks for reading. I hope that this was helpful!

--

--