Change The Way You Write Python Code With One Extra Character
one small syntax change, one giant step for your coding skills
The Syntax Language
Python is widely known for its simplicity, easy syntax, which requires basic English to understand, which made it so famous.
The simplicity of Python is what makes it so appealing for beginners, no declaration, without fancy words, or weird syntax. It continues with cools features, like decorators, and list comprehensions that do work wonders, but the *(asterisk) deserves the same spot, and I’m here to show you why.
Getting the ball rolling
I’ll start with a small trick:
Easy Way To Combine Dicts!
Now as you can easily see, I just concatenated to dictionaries with just a few asterisks, I’m going to explain everything, but I just wanted to show you what you have been missing so far!
Where Does the Astrix do?
Besides its well know usage for multiplication, the Astrix lets you do something pretty significant (and convenient) called unpacking.
You can use asterisks to unpack an iterable, and double unpack if it’s a two way iterable (like a dictionary).
Don’t Break Someone Else’s Code
This usage is more commonly known, but still underused.
Every time a developer writes a function, the function has a signature.
If the function changes, every piece of code written by someone else based on your code, will break.
Args, Kwargs
Args and Kwargs is a simple method to add functionality to your functions, without breaking its backward compatibility, resulting in more modular code.
Your function receive *args, **kwargs
as an input, which unpacks the entire extra input into the function.
Single Astrix is for standard iterable, double Astrix is for dictionary type, as always — an example:
This Example demonstrates how you can use args and kwargs to receive future arguments, for future use, without breaking old calls to your functions, this is super important!
if you have some spare read time — id recommend you’ll read this:
Sum Up
The Astrix is a significant part of Python and doesn’t get the honor and reputation it deserves. I hope this article helped even just a bit to change it.
If you found this interesting, or have a topic you wish to read about, please do comment.
I Hope You Enjoyed It!
'Data Analytics(en)' 카테고리의 다른 글
7 Awesome Command-Line Tools (0) | 2020.10.28 |
---|---|
Master Python Lambda Functions With These 4 Don’ts (0) | 2020.10.27 |
Data-Preprocessing with Python (0) | 2020.10.25 |
Advanced Python: 9 Best Practices to Apply When You Define Classes (0) | 2020.10.24 |
Tutorial: Stop Running Jupyter Notebooks from your Command Line! (0) | 2020.10.23 |