Answering a few Pythonic questions

  • 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 exec function and how we can use it?
  • What are metaclasses and dataclasses in python?
  • Is python interpreted or compiled? Are there ways to compile the code.
  • What is __call__ method in python?

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?

  • We should try to reduce the memory footprint of the program.
  • Many builtin functions are written internally in C. Which is a much faster language. So we should try using these whenever possible. For example using a for loop to calculate the maximum should be replaced by using max().
  • Also let’s say you are using numpy array and you don’t need any other function from that library. So
  • Talking about data science perspective we should try using numpy arrays over lists. This is because numpy arrays are created in contiguous locations like C unlike lists.

What is exec function and how we can use it?

  • In the first exec command to print the “x” I did not specify the global variable x so it uses it by default to print “5*x”.
  • In the next example to print the variable y. I specified it’s value as a local variable. This is the reason why it prints the value.

What are metaclasses and dataclasses in python?

Metaclasses

Dataclasses

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

What is __call__ method in python?

Reference links

--

--

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