Skip to content

Commit

Permalink
Document , fixes RafeKettler#24
Browse files Browse the repository at this point in the history
  • Loading branch information
RafeKettler committed Apr 8, 2012
1 parent e311532 commit 3c8a8b2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
5 changes: 4 additions & 1 deletion magicmethods.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ Without any more wait, here are the magic methods that containers use:
: Defines behavior for when an item is accessed, using the notation `self[key]`. This is also part of both the mutable and immutable container protocols. It should also raise appropriate exceptions: `TypeError` if the type of the key is wrong and `KeyError` if there is no corresponding value for the key.

`__setitem__(self, key, value)`
: Defines behavior for when an item is assigned to, using the notation `self[key] = value`. This is part of the mutable container protocol. Again, you should raise `KeyError` and `TypeError` where appropriate.
: Defines behavior for when an item is assigned to, using the notation `self[nkey] = value`. This is part of the mutable container protocol. Again, you should raise `KeyError` and `TypeError` where appropriate.

`__delitem__(self, key)`
: Defines behavior for when an item is deleted (e.g. `del self[key]`). This is only part of the mutable container protocol. You must raise the appropriate exceptions when an invalid key is used.
Expand All @@ -436,6 +436,9 @@ Without any more wait, here are the magic methods that containers use:
`__contains__(self, item)`
: `__contains__` defines behavior for membership tests using `in` and `not in`. Why isn't this part of a sequence protocol, you ask? Because when `__contains__` isn't defined, Python just iterates over the sequence and returns `True` if it comes across the item it's looking for.

`__missing__(self, key)`
: `__missing__` is used in subclasses of `dict`. It defines behavior for whenever a key is accessed that does not exist in a dictionary (so, for instance, if I had a dictionary `d` and said `d["george"]` when `"george"` is not a key in the dict, `d.__missing__("george")` would be called).

####An example####

For our example, let's look at a list that implements some functional constructs that you might be used to from other languages (Haskell, for example).
Expand Down
2 changes: 2 additions & 0 deletions magicmethods.tex
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ \subsection{The magic behind containers}
Called to implement behavior for the \code{reversed()} built in function. Should return a reversed version of the list.
\item[\code{__contains__(self, item)}]
\code{__contains__} defines behavior for membership tests using \code{in} and \code{not in}. Why isn't this part of a sequence protocol, you ask? Because when \code{__contains__} isn't defined, Python just iterates over the sequence and returns \code{True} if it comes across the item it's looking for.
\item[\code{__missing__(self, key)}]
\code{__missing__} is used in subclasses of \code{dict}. It defines behavior for whenever a key is accessed that does not exist in a dictionary (so, for instance, if I had a dictionary \code{d} and said \code{d["george"]} when \code{"george"} is not a key in the dict, \code{d.__missing__("george")} would be called).

\end{description}

Expand Down

0 comments on commit 3c8a8b2

Please sign in to comment.