Python Is Fun: Tricks of the Trade

Kanishk Varshney
3 min readJul 13, 2021
Photo by Hitesh Choudhary on Unsplash

Indisputably, Python is the most used language in modern programming community. Its intelligibility, robustness, and versatility makes it a great choice for beginners as well as experts. If you follow PYPL index, as of July 2021, Python is way ahead in terms of popularity ( >30% share from a plethora of languages). With over 1500 active contributors, the language provides some of the most interesting out-of-the-box tools for its community. In this blog, I will be introducing to three of my most favourite python tools that I frequently use in my development.

Type-hinting

For some of you this might be new, and many might be like what’s new about this. As the name suggests, it provides a C/C++ like type definitions for the python variables etc. The syntax looks something like following:

def greeting(name: str) -> str:
greet: str = 'Hello '
return greet + name

And the fascinating part, you can use this to catch development time errors.

You can further enhance the type-hinting by using typing module to add more details to you code.

We just changed Python from being an interpreted language to compile language 😜 (just kidding, don’t kick me for it! But this does help in writing better quality code)

Walrus Operator

Python3.8 came with a lot of new features, the most useful (and funny) one being the Walrus operator :=

Walrus Non-Operator

Yes, your wild guess is correct. It is called Walrus because of this only (Walrist much? 🤷‍♂)

This is a new type of assignment expression that allows you to assign and return a value in the same expression

Assume your piece of code:

def is_big_room(l, b):
area = l*b
if area >= 10:
big_room = True
print(big_room)

can now simple be written as:

def is_big_room(l, b):
area = l*b
print(big_room := area >= 10) #Assigns True to big_room on the fly

If you are feeling a bit more geeky, go ahead and skim through the PEP-0572

Dunder Methods

Saving the best for the last, Dunder or Magic methods. Dunder methods are special methods, preceded and succeeded by double underscores, that can override the functionality of build-in function or class methods.

Fun fact: Everything in python is an object, so you can override the functionality of everything, in one way or another

class Dunder:
def __init__(self, name: str, age: int, height: float):
self.name = name
self.age = age
self.height = height

def __repr__(self):
return f"{self.name.capitalize()}, {self.age}"

def
__len__(self):
return self.height


dunder = Dunder("JohnDoe", 34, 178)
print(f"Hi, I am {dunder}")
print(f"My height?: {len(dunder)}")

Or, you can override the default list behaviours:

class Liar(list):
def __len__(self):
return super().__len__() + 5


ll = Liar([1, 2, 3])
print(len(ll)) # prints 8

Hopefully, these tools will help you enrich your code as well!

Thank you folks! That was my time, you all have been a wonderful audience!

--

--

Kanishk Varshney

Data Scientist | Artist | Easing into Life | Buy me a coffee!