[Tutor] Where's the Walrus?

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Tue, 20 Feb 2001 23:19:56 -0800 (PST)

On Tue, 20 Feb 2001, Tim Johnson wrote:

> FYI: Am coming from a background in C++
> but please don't hold that against me :)

That's no problem; don't worry about it.  Let's take a look... oh!  Take a
look here:

> ally_oop = schiz()
> print ally_oop
> ally_oop.next_personality
> print ally_oop

"ally_oop.next_personality" refers to the next_personality method, which
is what you want to call.  But you need something more to tell Python to
execute that function:


Even if it doesn't take in any arguments, "ally_oop.next_personality" has
a different meaning to Python as opposed to "ally_oop.next_personality()":
the first gives us the function itself as a value, while the second
invokes that function.

(Random note: in Matlab, functions that don't take one argument force you
NOT to include the parentheses.  Really evil language.  Grrr.  Back to

Here's a small interpreter session that tries to show the difference
between function as a "process" and function as a "value":

>>> def sayHello(name):
...     print "hello", name
>>> sayHello('Tim')
hello Tim

>>> sayHello
<function sayHello at 0x81cbd9c>     # Hey!  So sayHello is a function

>>> greet = sayHello
>>> greet('Tim')
hello Tim

Being able to treat functions themselves as values is neat, because now we
can give different names to functions.  (You might recognize this as
"function-pointer" stuff in C++, but in a simpler presentation.)  That's
not the main advantage of using functions as values, but it's one of them.

Feel free to ask any questions to us.  It's good to find another C++'er
among us.  *grin*