Next, Function or Method ?

While in Python 2 it was possible to use both the function next() and the .next() method to iterate over the resulting values of a generator, the later has been removed with the introduction of Python 3. Consider the sample generator: def sample_generator(): yield "a" yield "b" yield "c" In Python 2: a = sample_generator() print(next(a)) # prints 'a' print(a.next()) # prints 'b' But in Python 3: print(next(a)) # prints 'a' print(a....

April 26, 2017 · 1 min · Rezha Julio

Lambda Functions in Python

The lambda keyword in Python provides a shortcut for declaring small anonymous functions. Lambda functions behave just like regular functions declared with the def keyword. They can be used whenever function objects are required. For example, this is how you’d define a simple lambda function carrying out an addition: >>> add = lambda x, y: x + y >>> add(5, 3) 8 You could declare the same add function with the def keyword:...

April 16, 2017 · 5 min · Rezha Julio

Function in Python are First-Class Object

Python’s functions are first-class objects. You can assign them to variables, store them in data structures, pass them as arguments to other functions, and even return them as values from other functions. Grokking these concepts intuitively will make understanding advanced features in Python like lambdas and decorators (I will cover two this in the next post) much easier. It also puts you on a path towards functional programming techniques. In this post I’ll guide you through a number of examples to help you develop this intuitive understanding....

April 6, 2017 · 8 min · Rezha Julio