Level Up Coding

Coding tutorials and news. The developer homepage gitconnected.com && skilled.dev && levelup.dev

Follow publication

Python: Uncovering the Overlooked Core Functionalities

Erik van de Ven
Level Up Coding
Published in
8 min readJul 24, 2023

Photo by Stefan Steinbauer on Unsplash

Update 25/07: Endless thanks for all the numerous tips, comments, and suggestions I received from the Hacker News community. I really appreciate it. I have incorporated some of them into this article. If you wish to read all of them, you can do so here.

What Kyle Simpson mentions about JavaScript in his books, Luciano Ramalho mentions in his book ‘Fluent Python’ about Python. They basically address the same issue of both languages. To put it in my own words:

Because the language is so easy to learn, many practitioners only scratch the surface of its full potential, neglecting to delve into the more advanced and powerful aspects of the language which makes it so truly unique and powerful

So let’s discuss briefly all functionalities you might haven’t heard of, but you definitely want to know if you aim to become a truly seasoned Pythonista.

Evaluation of Default Arguments

Python arguments are evaluated when the function definition is encountered. Good to remember that! This means that each time the fib_memo function (which is mentioned below) is called without explicitly providing a value for the memo argument, it will use the same dictionary object that was created when the function was defined.

def fib_memo(n, memo={0:0, 1:1}):
"""
n is the number nth number
you would like to return in the sequence
"""

if n not in memo:
memo[n] = fib_memo(n-1) + fib_memo(n-2)
return memo[n]

# 6th Fibonacci (including 0 as first number)
fib_memo(6) # should return 8

So this code works, in Python. This also means that you could execute the fib_memo function in a single script multiple times, like in a for loop, with each execution increasing the fibonacci number to be computed, without hitting the “maximum recursion depth exceeded” limit, as the memo will keep expanding. More information can be found in my other article.

Important note (thanks to the Hacker News community)

Keep in mind that, although the mutable default arguments (as mentioned above) can lead to less code, not everyone might be aware of its behavior. It might even lead to hardly solvable bugs. Some even…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Written by Erik van de Ven

Erik is a Senior SE with 15+ years of experience in programming and 8+ years in Python. He ranked the top 9% on Stack Overflow and is a Kaggle Expert.

Responses (4)

Write a response

A table which shows the differences of outputs depending on the values in the iterable, is shown below.

Hopefully the table cell saying that any() returns True with "One False value" is incorrect.
In case it's correct... WAT.

--

Amazing, Thank you for sharing :)

--

There's a typo in the multiple with statements point, the second file you open should be opened in 'w'rite mode, to be able to write to it.

--