[Courses] [python] Object-oriented programming
Akkana Peck
akkana at shallowsky.com
Sun Jul 31 17:56:27 UTC 2011
Leslie writes:
> Hi. This is a question pertaining to Lesson 7.
> It is about functions I find in python 'help'.
>
> In interactive mode, let's say I create a simple list named 'mylist'.
> I type dir() and I can see that it is there.
> I type help (mylist).
dir() is a good thing to know about -- I should have mentioned that.
It gives you a list of all the functions in a class.
> This puts me in Help on list objects.
> First I get some methods (__add__, __contains__, and so on).
> Then come entries which do not have double underscores on either side
> (append, count, etc.).
>
> I have learned from Lesson 7 how to use the latter (e.g., mylist.append,
> mylist.count).
>
> Here is my question. I don't understand how to use the ones with double
> underscores around them. The only example we saw of such a creature in
> this lesson is __init__.
The double-underscore things are generally functions that aren't
called by name, but are called in response to some other operation.
For instance, you know __init__() is called when you create an object
of that class.
So if you look at dir(mylist), you'll see things like __add__,
which gets called if you add two lists. If you say
>>> mylist = [1, 2]
>>> mylist + [3, 4]
[1, 2, 3, 4]
then internally, when Python sees the +, it looks for an __add__()
function in the list class, and calls it.
__contains__() is anothe one you'll see there: it gets called if you
do something like "if 2 in mylist:"
>>> 2 in mylist
True
You can call those double-underscore functions directly if you
want to, though generally you won't need to. This is the same as
the previous "2 in mylist":
>>> mylist.__contains__(2)
True
You can probably guess what a lot of the special functions do, like
__lt__ and _gt__ (corresponding to the < and > operators),
__mul__ (the * operator), and __len__ (len(object)). Others are
not so obvious. Here's a long list of these special
double-underscore functions you can define if you want them:
http://docs.python.org/reference/datamodel.html#basic-customization
One important one that a lot of classes need is a way of converting
themselves to a string. If you're debugging and you want to know
what your object contains, and you print myobject, you'll typically
get something like
<__main__.MyClass instance at 0x91491ec>
Not very helpful when you want to know what's inside it! But you
define the special class function __repr__() (for "representation) to
return a readable string representation of your object:
class FlashCardViewer:
def __repr__(self):
Return "FlashCardViewer containing %d flashcards" % (self.num_cards)
You may have noticed that there's also a __str__() function for
converting your class to a string. But __repr__() is more general
than __str__() -- it works for "informal" conversions too, like if
you're debugging in the Python console and want to see what's inside
your object, as well as places where someone formally calls
str(object) to convert to a string. If you define __repr__, it will
work everywhere __str__ will plus more.
Thanks for asking this question! I'd long been meaning to look for
the full list of double-underscore functions but needed the push to
go find it, so this will help me too. :-)
...Akkana
More information about the Courses
mailing list