Python get function default arguments

Author: Dzimulik Date: 11.06.2017

Recently I found a nasty bug in some Python code due to a misuse of default arguments. If you already know everything about default arguments and just want to laugh at the funny error feel free to jump to the end of the article.

Alas, the code was written by me, but I'm pretty sure that day I was replaced by an evil clone of myself. You know, it happens sometimes. This post just summarizes the basics about standard and default arguments in Python functions and warns against possible pitfalls in your code. If you are just approaching Python and started writing functions I warmly recommend checking what the official Python manual says about them at the following links: Defining Functions and More on Defining Functions.

Gotcha — Mutable default arguments | Python Conquers The Universe

Python is a strongly object-oriented language, pushing this programming paradigm to its maximum. But object-oriented programming still relies on the function concept, that is something you can use to process data. Python has a more general concept of callable object, that is every object that can be called, which means applied on some data.

Functions in Python are callable objects and at a first glance they behave the same as functions in other languages.

They just get some data, called arguments , process them, and return a result which is None if no return statement is issued. Arguments are declared when the function is defined as placeholders for actual objects that will be passed when the function will be called.

In Python you do not declare the type of an argument like you do in C or Java, for example since Python philosophy relies on polymorphism. Remember that Python variables are references , that is memory addresses of actual variables.

sys — System-specific parameters and functions — Python rc1 documentation

When you call a function you are not copying the value of the arguments to the function placeholders. Instead, you are pointing the placeholder to the variable itself. This has a very important consequence: A good visual explanation of the reference mechanism in Python is this. References play a very big role in Python, being the bare bones of its fully-polymorphic approach.

Check this link for a better explanation of this very important subject.

python get function default arguments

To check your understanding of this basic behaviour of the language try to follow this simple code where the variable ph stands for "placeholder".

If you haven't been surprised by what's happening here you already grasped one of the most important things in Python and can safely skip the following explanation. Indeed both act the same, trying to modify the value of the original variable passed, but some types in Python are immutable , and integers are among them. On the other hand, lists are not immutable, so the function does what its name pledges.

There is a lot more to say about functions in Python, but these are the basic bricks about standard arguments. Every now and then you need to define a function that may accept an argument and shall behave differently whether or not the argument is present. If a language does not provide support for such cases you only have two choices: Both solutions are viable but suboptimal.

Python, like other languages, provides support for default argument values, that is function arguments that can either be specified by the caller or left blank to automatically receive a predefined value. You can find more interesting examples in the standard library, for example in the open function check the official documentation.

As you see from the open builtin function we can use both standard and default arguments in a function, but the order in which they appear in the function prototype is fixed: The reason is easy to figure out: For example consider the following function definition. That order of definition is thus forbidden, and if you try Python will raise a SyntaxError. Default arguments may be provided as plain values or as the result of a function call, but this latter technique need a very big warning.

While plain values are hardcoded, thus needing no evaluation except that made at compilation time, function calls are expected to be executed at run time check this comment on Reddit for a better explanation of this matter. So we could write. This unfortunately does not work: As suggested by this latter resource the usual solution is to replace the default value with None and to check the value of the argument inside the function.

Default Parameter Values in Python

Default arguments may vastly simplify APIs, provided that you pay attention to their only "failure point", the evaluation time. Surprisingly, one of the most basic things in Python, function arguments and references, are one of the biggest source of errors, sometimes for experienced programmers too. I recommend giving references and polymorphism some study time. I added the link to the commented section.

Content licensed under a Creative Commons Attribution-ShareAlike 4. Toggle navigation The Digital Cat. About Programming Projects Archives.

Rating 4,6 stars - 385 reviews
inserted by FC2 system