Let’s discuss some Interesting questions regarding python

Image for post
Image for post

Python is an interpreted, high-level and general-purpose programming language. Python’s design philosophy emphasizes code readability with its notable use of significant whitespace. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.

Python is dynamically typed and garbage-collected. It supports multiple programming paradigms, including structured (particularly, procedural), object-oriented, and functional programming. Python is often described as a “batteries included” language due to its comprehensive standard library.

In this article, I will answer some common and interesting question

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

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

→ How can we get the code of a python object?

→ What are the ways to make python code work faster?

→ What is the exec function and how we can use it?

→ What are metaclasses and data classes in python?

→ What is __call__ a method in python?

So let's start with our very first question

But for this, we must know what is complied and interpreted signifies.

  • Compiled language: A compiled language is a programming language whose implementations are typically compilers (translators that generate machine code from source code), and not interpreters (step-by-step executors of source code, where no pre-runtime translation takes place).
  • Interpreted language: An interpreted language is a type of programming language for which most of its implementations execute instructions directly and freely, without previously compiling a program into machine-language instructions. The interpreter executes the program directly, translating each statement into a sequence of one or more subroutines, and then into another language (often machine code).

Python is an interpreted language and not a compiled one, although compilation is a step. Python code, written in .py file is first compiled to what is called bytecode which is stored with a .pyc or .pyo format.

To compile a Python program into an executable, use a bundling tool, such as Gordon McMillan’s installer (alternative download) (cross-platform), Thomas Heller’s py2exe (Windows), Anthony Tuininga’s cx_Freeze (cross-platform), or Bob Ippolito’s py2app (Mac). These tools put your modules and data files in some kind of archive file and create an executable that automatically sets things up so that modules are imported from that archive. Some tools can embed the archive in the executable itself.

Lists are actually pretty quick when you are accessing an item by its index number since all that happens under the hood is direct access to a known and easy to calculate memory location. Lists though can be very slow when searching (using in) 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.

A dictionary utilises a data structure called a hashmap (Python dictionaries are optimised versions), and a key will be converted using a hash algorithm from a string (or whatever) into an integer value, and it is a couple of very simple calculations to take that integer and find the right place in the dictionary to look.

One way to think about it is the difference between a list and a dictionary is a difference between a large set of bookshelves vs a library. To find a book across a set of shelves you have to search each shelf one at a time, whereas a library has an index that allows the reader to go from the title of a book directly to a specific shelf. In a Python dictionary, it is the ‘hash algorithm’ that acts as the library index.

The inspect module provides several useful functions to help get information about live objects such as modules, classes, methods, functions, tracebacks, frame objects, and code objects. For example, it can help you examine the contents of a class, retrieve the source code of a method, extract and format the argument list for a function, or get all the information you need to display a detailed traceback.

Image for post
Image for post

We can see python printed all the code of the variable using the inspect module but there are a lot more things we can achieve using the inspect module here the link for more details on the inspect module.

There are few ways by which one can increase the performance of python code

  • Using generators & sorting with keys
  • Avoid unwanted loops
  • Keep Python code small and light
  • Try out multiple coding approaches
  • Using the latest releases of Python
  • Use some of Python’s “speedup” applications

For further depth details on these, points you can refer this link

Exec function can dynamically execute code of python programs. The code can be passed in as string or object code to this function. The object code is executed as is while the string is first parsed and checked for any syntax error. If no syntax error, then the parsed string is executed as a python statement.

There are four main kinds of services provided by this module: type checking, getting source code, inspecting classes and functions, and examining the interpreter stack.

Passing String: we will pass a single line of code as a string to the exec() function.

Image for post
Image for post

As we can see It gets parsed and executed to give the output.

Passing Code Object: Now we see how to pass a block of code with multiple code statements.

Image for post
Image for post

As it is a code object, it gets executed directly giving the result. Please note how we have used \n and space to create a python code block with proper indention. We have used input function which will take input on the runtime and then the code is getting executed dynamically.

A metaclass in Python is a class of a class that defines how a class behaves. A class is itself an instance of a metaclass. A class in Python defines how the instance of the class will behave. To understand metaclasses well, one needs to have prior experience working with Python classes. Before we dive deeper into metaclasses, let’s get a few concepts out of the way.

Image for post
Image for post

Data class 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 Dataclsses are implemented by using decorators with classes. The data class provides an inbuilt __init__(), constructor, to classes which handle the data and object creation for them.

__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.

Image for post
Image for post

Hopefully, you learn something new from the article as well as enjoy it.

Thanks for reading this article! Leave a comment below if you have any questions.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store