python function parameters, debugging, comments, etc.

Rotwang sg552 at hotmail.co.uk
Tue Oct 1 19:45:55 EDT 2013


On 01/10/2013 23:54, Chris Friesen wrote:
>
> I've got a fair bit of programming experience (mostly kernel/POSIX stuff in C).  I'm fairly new to python though, and was hoping for some advice.
>
> Given the fact that function parameters do not specify types, when you're looking at someone else's code how the heck do you know what is expected for a given argument?  (Especially in a nontrivial system where the parameter is just passed on to some other function and may not be evaluated for several nested function calls.)
>
> Is the recommendation to have comments for each function describing the expected args?
>
> [...]

In the Python community, one of the programming styles that is 
encouraged is "duck-typing". What this means is that rather than writing 
functions that check whether arguments passed to that function are of a 
specific type, the function should simply use any methods of those 
arguments it requires; that way the function will still work if passed 
an argument whose type is a custom type defined by the user which has 
the right interface so that the function body still makes sense (if it 
quacks like a duck, then the function might as well treat it like a 
duck). If a user passes an argument which doesn't have the right methods 
then the function will fail, but the traceback that the interpreter 
provides will often have enough information to make it clear why it failed.

(see http://docs.python.org/3/glossary.html#term-duck-typing )


So the upside of duck-typing is clear. But as you've already discovered, 
so is the downside: Python's dynamic nature means that there's no way 
for the interpreter to know what kind of arguments a function will 
accept, and so a user of any function relies on the function having 
clear documentation. There are several ways to document a function; 
apart from comments, functions also have docstrings, which will be 
displayed, along with the function's signature, when you call 
help(function). A docstring is a string literal which occurs as the 
first statement of a function definition, like this:

def foo(x, y = 2):
     '''This function takes an argument x, which should be iterable, and
a function y, which should be a numeric type. It does nothing.'''
     pass


If I call help(foo), I get this:

Help on function foo in module __main__:

foo(x, y=2)
     This function takes an argument x, which should be iterable, and
     a function y, which should be a numeric type. It does nothing.


In Python 3.0 and later, functions can also have annotations; they have 
no semantics in the language itself but third-party modules can use them 
if they choose to do so. They look like this:

def foo(x: str, y: int = 2, z: 'Hello' = None) -> tuple:
	return a, b, c

For more about annotations, see here:

http://www.python.org/dev/peps/pep-3107/


So the short answer is that Python gives you several methods for making 
it clear what kind of arguments the functions you define should be 
passed, but unfortunately you'll likely encounter functions written by 
people who made no use of those methods. On the plus side, Python's 
exception reporting is good, so if in doubt just try using a function in 
the interactive interpreter and see what happens (with the usual caveats 
about using untrusted code, obviously).



More information about the Python-list mailing list