Functional Particularities of Python

This time we’ll explore some of the paradigms of functional programming as applied to Python, specifically. One of the most common constructs to employ in a functional-style Python program is a Generator. Generators are a special class of functions that make writing iterators [1] more easy. When we call a function, a new space in memory is allocated to hold all of the variables that function is concerned with, as well as other data....

July 19, 2018 · 3 min · Rezha Julio

New interesting data structures in Python 3

Python 3’s uptake is dramatically on the rise rise these days, and I think therefore that it is a good time to take a look at some data structures that Python 3 offers, but that are not available in Python 2. We will take a look at typing.NamedTuple, types.MappingProxyType and types.SimpleNamespace, all of which are new to Python 3. typing.NamedTuple typing.NamedTuple is a supercharged version of the venerable collections.namedtuple and while it was added in Python 3....

May 25, 2017 · 4 min · Rezha Julio

Keyword argument demystify

There’s a lot of baffling among Python programmers on what exactly “keyword arguments” are. Let’s go through some of them. If you somehow are writing for a Python 3 only codebase, I highly recommend making all your keyword arguments keyword only, especially keyword arguments that represent “options”. There are many problems with this sentence. The first is that this is mixing up “arguments” (i.e. things at the call site) and “parameters” (i....

May 22, 2017 · 3 min · Rezha Julio

Looping techniques in Python

Python has multiple techniques for looping over data structures. Dictionary looping with both key and value can be done using the items() method: my_dict = {'first': 'a', 'second': 'b'} for k, v in my_dict.items(): print(k, v) # first a # second b The enumerate() function allows looping with both index and value through any sequence: my_list = ['a', 'b'] for i, v in enumerate(my_list): print(i, v) # 0 a # 1 b zip() function can be used to pair two or more sequences in order to loop over both of them in parallel:...

May 4, 2017 · 1 min · Rezha Julio

Enhance your tuples

Standard Python tuples are lightweight sequences of immutable objects, yet their implementation may prove inconvenient in some scenarios. Instead, the collections module provides an enhanced version of a tuple, namedtuple, that makes member access more natural (rather than using integer indexes). Import namedtuple: from collections import namedtuple Create a namedtuple object: point = namedtuple('3DPoint', 'x y z') A = point(x=3, y=5, z=6) print(A) # 3DPoint(x=3, y=5, z=6) Access a specific member:...

May 3, 2017 · 1 min · Rezha Julio