Function Arguments in Julia and Python

Variable number of arguments, default arguments etc

This is not a complicated topic, but it is something very useful to be aware of because it can simplify the way you write code a lot. Variable number of named arguments e.g. gives you the ability to create what looks like domain specific languages.

Once I created a library for specifying GUI layout for Qt widgets, that looked something like this:

The challenge is how do you write code looking something like this:

You may not know when creating this code how many attributes some UI component can have, and you don’t really care either. You just want to output them say in a JSON format or XML. This is where variable named arguments comes in handy.

Named Arguments

Here is an example of an attempt at implementing something like this in Julia:

This works for setting up default argument values:

However it does not work for named arguments:

Python OTOH interprets this differently.

I have no problems calling this with named arguments:

However I cannot call with named arguments I did not already name in the argument list:

Before looking at the solution, let us look at how named arguments are handled in Julia. Python is different in that every argument is potentially a named one. In Julia however named arguments have to be separated from the rest with a semicolon ;.

In this case name is a regular argument with default value "noname" while caption and enable are named arguments with default values.

You could call it like this:

The order of unnamed arguments matter but not the order of named arguments.

Variable Number of Arguments

Julia uses ... as a suffix to indicate variable number of arguments for both normal and named arguments, while Python uses both * and **. It needs two symbols because Python does not separate named variables form the rest.

Here is an example of functions returning the arguments as two separate arrays in Julia.

The names args and kwargs are not important. I could have written instead:

Strongly speaking these are not arrays, but iterators. But we can Julia’s collect function to turn them into arrays.

Notice how collect(d) gives us an array of pairs. In Julia pairs are formed with key => value syntax. :four and :five are symbols rather than strings. In LISP tradition Julia like LISP, Scheme and Ruby use extensively symbols. A symbol is essentially an immutable string which there are only one of, and which has to be a valid identifier in a programming language. You cannot throw in spaces e.g. Python will tend to use regular strings instead of symbols.

Let us look at the python version. The call looks almost identical:

However Python does not return some sort of iterators which we need to collect values from. Instead we get a tuple for regular arguments.

And a dictionary for the named arguments. Notice the keys are just strings, not symbols.

Unpacking Arrays to Arguments

This is essentially the reverse problem of turning a list of arguments into an array. Instead consider the case where all the arguments are contained within an array, and we want to pass these arguments to a function.

In Julia we need to use ; when calling the function this way to separate arguments and keyed arguments.

In Python this is a bit more symmetrical, since using * and ** makes it easy to distinguish regular and keyed arguments:

List Comprehensions

If you don’t know what a list comprehension is, it is easier to just give an example.

You can see that we basically do a for-loop and each element in the loop is squared to give the value of each element in an array. It is almost the same in python:

In fact we can even filter in the same way:

But it goes deeper than that, in both Julia and Python the comprehension syntax is not limited to defining lists or arrays. In fact in both cases a generator object is created. It is the generator which provides values to construct the array.

So I e.g. use it in Julia to create an array of pairs to initialize a dictionary:

To understand how this works, let us construct a simple function in both Julia and Python to extract the object created by the comprehension.

If we check the type, it gets a bit complicated because Julia supports parameterized types:

What this basically says is that a comprehension is of type Generator, which is Julia's Base package. Generators support Julia's iteration interface. Anything you want to be able to iterate over in e.g. a for loop, must implement the iterator interface.

Let me show you have this works in Python, and you will see it follows a very similar logic.

Except the Python generators mutates when it is used. Thus it gets exhausted and you cannot call next on it the way you can call iterate on the Julia generator:

The state of the iterator is not kept outside in a state object like with Julia. This we need to recreate the generator:

To understand more about the differences I would need to get into generators, but we’ll leave that for next time.

Geek dad, living in Oslo, Norway with passion for UX, Julia programming, science, teaching, reading and writing.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store