[Tutor] __setitem__ [classes and functions]
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Fri Feb 27 16:28:31 EST 2004
On Fri, 27 Feb 2004, Christopher Spears wrote:
> Thanks! I do have a question though regarding this statement:
>
> > A class is what a programmer uses to create objects.
>
> This is confusing to me because I always thought of classes as another
> type of data object like strings or dictionaries except a class is an
> object that can hold variables, functions, etc. like a file (which is
> also seen as an data object by Python, right?).
Hi Chris,
Here's one perspective that might help: A class is, in some aspects, very
similar to a function. Functions are things that we can manipulate:
###
>>> def makeEmptyDictionary():
... return {}
...
>>> makeEmptyDictionary
<function makeEmptyDictionary at 0x8158a5c>
###
One neat thing about objects in Python is that they can be squirreled away
in lists or dictionaries:
###
>>> [makeEmptyDictionary, makeEmptyDictionary]
[<function makeEmptyDictionary at 0x8158a5c>,
<function makeEmptyDictionary at 0x8158a5c>]
###
This isn't as useless as it might first appear: a variation of this is a
core idea behind the "switch" statement in other programming languages.
For example, the following snippet:
###
>>> def add(x, y): return x + y
...
>>> def sub(x, y): return x - y
...
>>> def math_eval(operation, x, y):
... dispatch_table = { '+': add,
... '-': sub }
... if operation in dispatch_table:
... operator = dispatch_table[operation]
... return operator(x, y)
... else:
... print "I don't know about", operation
... return None
...
>>> math_eval('+', 3, 17)
20
>>> math_eval('-', 49, 17)
32
>>> math_eval('-', 49, 7)
42
###
uses a dictionary whose keys are functions. (Sorry for the detour!
*grin*)
Anyway, so functions are objects. But not only can these functions can
also be stored or passed around, but they can also be called ("applied"):
###
>>> makeEmptyDictionary()
{}
###
And this function "calling" is traditionally what we usually do with
functions, and we usually expect a function call to return back to us some
object.
It turns out that since functions are objects, it's perfectly possible to
have a function whose return value is itself a function:
###
>>> def getOperator(operation):
... dispatch_table = { '+': add,
... '-': sub }
... if operation in dispatch_table:
... return dispatch_table[operation]
... else:
... print "I don't know about", operation
... return None
...
>>> getOperator("+")
<function add at 0x8159704>
>>> getOperator("-")
<function sub at 0x81584c4>
###
but let's not get into that too deeply yet. *grin*
Anywa, so just as we defined a function, we can create a class:
###
>>> class MyClass:
... def __init__(self):
... pass
...
>>> MyClass
<class __main__.MyClass at 0x8158b4c>
###
This class, too, is itself an object, and can be passed around as
parameters, stored in lists, and returned by functions:
###
>>> def getSomeClass(password):
... if password == 'foo':
... return MyClass
... else:
... print "No, you need to say the magic word."
... return None
...
>>> c = getSomeClass('foo')
>>> c
<class __main__.MyClass at 0x8158b4c>
###
And just as we did that math_eval() example by pulling out the right
function from a dictionary, we can do something similar with classes! We
can keep a list of classes in some dictionary, and return any particular
class.
These classes, too, can be applied, and their return values:
###
>>> MyClass()
<__main__.MyClass instance at 0x8154254>
>>>
>>>
>>> c()
<__main__.MyClass instance at 0x8154264>
###
... are "instances".
The return value that comes off a class is itself another object, but we
try to be more formal about it can call it an "instance" to further
describe the thing we're getting back. An "instance" is a label we attach
to object that's was born by a class.
Anyway, hope this helps!
More information about the Tutor
mailing list