[Tutor] On understanding defintions

Dave Angel d at davea.name
Tue Dec 4 23:09:22 CET 2012


(responding on-list to an offline question)


On 12/04/2012 04:18 PM, doark at mail.com wrote:
> Here's a better question. When is it appropriate to use a def statement?
> 

Welcome to the Python tutor mailing list.

A function is usually defined by a def statement and the suite of
indented lines that follow.  (There are other ways to get a function
object, including lambda, but that's an advanced topic)

A function may be called from other places in the code, and optionally
arguments may be passed to that function and/or returned from that function.

There are many reasons to wrap one or more lines of code into a function
and call it, but the most important is one of clarity.  If you give it a
good name, and understand what it does, then the place where it is
called becomes easier to understand.

For example, somebody wrote the function  math.tan().  It takes one
argument, and returns one result.  And the work it does is to calculate
a tangent of an angle given in radians.  The function is not part of
python the language, but it is part of the standard library.

When I write a program that needs that work done, rather than including
the complexity in each place, I refer to the one that was written (by
someone else), and my code is much clearer for it, because anyone
reading my code knows (or can find out) what math.tan() does.

The same applies for some code you write strictly for your own use.
Even if the whole program is in one file, it's still useful to
encapsulate it into distinct parts, most of which perform some fairly
simple set of operations.

A second reason is shortening of the code.  If you write a (non-trivial)
function once, and call it from ten places, then your code is shorter by
9 copies of the function's body, more or less.  That might not sound
like a big deal, but it also means that if you find a problem with the
algorithm, you can fix it in one place, and not have to hope that you
got all ten places where you made the same error.

Your next question is apparently what the elements of the 'def'
statement mean.

The simplest type of function might be like:

def  print_separator():
    print ("----------")

This function takes no arguments, and returns no results.  But it does
something useful.  You call it with
    print_separator()

So you might have a set of code something like

    print(value1)
    print_separator()
    print(value2)
    print_separator()
    print(math.tan(value99)

Let's suppose you later decided to change the dashes to some other
character.  You could just change the body of the def, and all the calls
would use the new string.

But most functions take one or more arguments.  So let's add some formal
parameters to our definition.  For now, we'll just use positional
parameters (leaving keyword parameters for chapter 6).

def print_separator(size):
     print("-"*size)   #print 'size' copies of the character '-'

This takes one argument and binds it to the formal parameter size. Then
size can be used inside the function, without caring how it was produced.

Now you can produce a different sized separator just by varying the
argument to this function.
    print(value1)
    print_separator(40)
    print(value2)
    print_separator(10 * len(value2))
    print(math.tan(value99)

Now, you could add a second parameter to the function, by just adding a
comma and name):

def print_separator(size, char):
     print(char * size)   #print 'size' copies of the specified character

Now the caller can specify both.  And in fact MUST supply both.  If
that's undesirable, then you can give defaults to one or both.

def print_separator(size=10, char="-"):

Now, if it's called with no arguments, it works like the first one.  If
it's called with one, it better be an integer.  And if it's called with
two, the first better be an integer and the second a string.

No idea if that's what you wanted, but if you want much more, you should
be working through a tutorial.  Be sure the version of tutorial matches
the version of Python you're using.  Same is true of those examples you
found "bugs" in.  Could be the page they're on was meant for a different
version of Python than what you're running.  For example, in Python 2.x,
'print' was a statement, while in 3.x it's a function.



>> ----- Original Message -----
>> From: Dave Angel
>> Sent: 12/04/12 02:19 PM
>> To: frank ernest
>> Subject: Re: [Tutor] On understanding defintions
>>
>> On 12/04/2012 01:29 PM, frank ernest wrote:
>>> Opensuse 12.2 python3.2
>>>  I discoverd that some of the examples for definitions in the tutorial are not valid. I am reporting this as a bug.
>>
>> And what are the bug numbers?  Or links to the bug reports?  Otherwise
>> why are you telling us about them without mentioning any details?
>>

To log a bug, or search for an existing one, go to
http://bugs.python.org/ .  Searching there, i don't see any bugs logged
by you.  So what did you mean by report?

If there are legitimate bugs, they certainly should be reported.  But
perhaps you might want to describe them here, to make sure they're not
just misunderstandings.



-- 

DaveA


More information about the Tutor mailing list