Elemental Questions of Python

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

Yeah ! Python is an interpreted language ( it means a computer program that translates and executes, line by line, a program written in a high level language), although compilation is a step. Python code, written in .py file is compiled to what is called bytecode and stored in .pyc format. This bytecode is a low level set of instructions that can be executed by an interpreter. The bytecode compilation happened internally, and almost completely hidden from developer. Bytecode instructions are executed on Virtual Machine.

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

Searching in a dictionary is way much faster than searching in a list, it is bcz Python implements dictionaries using hash tables.

Dictionaries are Python’s built in a mapping type and so have also been highly optimized, list requires walking through the list until it finds the result from beginning to the result each time .However, if the data sets are really small (<1000 elements) list perform well.

How can we get the code of a python object ?

We can get the source code of a python object ,we have inspect module a built-in standard library in Python. getsource() method is used to get the source code of Python objects. It returns the text of the source code of an object. The argument may be a module, class, method, function, or code object.

What are the ways to make python code work faster ?

jhdfn

Use list comprehensions : They are concise and speedy way to create new lists.

Use “in” if possible : To check if membership of a list, it’s generally faster to use the “in” keyword.

Use Sets and unions : Too much looping makes your code unhealthy, if you wanted to get the overlapping values in two lists. You could do this using nested for loops.

Use multiple assignments : Python has good way to assign the values of multiple variables.

Use join() to concatenate strings : In python, you can concatenate strings using “+” . However, strings in Python are immutable, and the “+” operation involves creating a new string and copying the old content at each step . A more efficient approach would be to use the array module to modify and then use the join() function to re-create your final string.

Use builtin functions and libraries : Python comes with a lot of batteries included. You can write quality and efficient code but it’s hard to beat the underlying libraries. These have been optimized and are tested.

What is exec function and how we can use it ?

exec() function is used for the dynamic execution of Python program which can either be a string or object code. If it is a string, the string is parsed as a suite of Python statements which is then executed unless a syntax error occurs and if it is an object code, it is simply executed.

Syntax:

exec(object[, globals[, locals]])

It can take three parameters:

  • object : As already said this can be a string or object code
  • globals : This can be a dictionary and the parameter is optional
  • locals: This can be a mapping object and is also optional

Example:

prog = 'print("The sum of 5 and 10 is", (5+10))'

exec(prog)

Output:
The sum of 5 and 10 is 15

What are metaclasses and dataclasses in python ?

Metaclass is a class whose instance classes. Like an “ordinary” class defines the behavior of the instances of the class, a metaclass defines the behavior of classes and their instances.

Detaclasses module is introduced in Python 3.7 . A dataclass is a class typically containing mainly data, although there aren’t really any restrictions. It is created using the new @dataclass decorator. Dataclasses automatically create __init__, __repr__, __eq__, __gt__, __lt__ methods without you having to add them explicitly. This is handy when we need to create custom containers for your data.

What is __call__ method in python ?

Python has a set of built-in methods and __call__ is one of them. Python object is called callable if they define __call__() function. The __call__ method enables Python programmers to write classes where the instances behave like functions and can be called like a function.

Thanks for reading. I hope that this was helpful!👍

Refrences:

http://net-informations.com/python/iq/how.htm

https://www.tutorialspoint.com/How-to-retrieve-source-code-from-Python-objects#:~:text=We%20use%20the%20getsource(),source%20code%20of%20the%20function.&text=Returns%20the%20text%20of%20the,returned%20as%20a%20single%20string.

https://rednafi.github.io/digressions/python/2020/06/26/python-metaclasses.html#:~:text=A%20metaclass%20is%20a%20class,of%20classes%20and%20their%20instances.&text=Python%20provides%20you%20a%20way,hood%20and%20define%20custom%20metaclasses.

https://www.geeksforgeeks.org/__call__-in-python/

--

--

--

The price of “anything” is the amount of “time”, U xchange for it. Education | Technology | Data Science | Statistics | History

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Comprehensions in Python

What is the Azure CDN : Article 03

Why serverless is a good choice for your business

Functional test coverage in Golang

Class Methods In Ruby: a Thorough Review & Why I Define Them Using class << self

C# — Async Pipeline using Observer Design Pattern

Adding Post Processing Profiles with Universal Render Pipeline (URP) in Unity

What is An API

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
Abu Qais

Abu Qais

The price of “anything” is the amount of “time”, U xchange for it. Education | Technology | Data Science | Statistics | History

More from Medium

A Deep Dive Into Python Decorators

Nice living room

The `getattr` function in Python

Replicated Acoustic Features Parkinson Database

How to Send SMS to Mobile Number using python