[NEWBIE] Sequence indices & method docstrings

Paul-Michael Agapow p.agapow at ic.ac.uk
Mon Sep 4 08:17:33 EDT 2000


While playing around Python, I've run across a mysterious point or two.
While these aren't holding me up in any way, I'm curious to understand
them.

The first concerns sequence indices.  It seems to me there are two
different ways in which they work. Under simple use (myList[4],
myList[-1]) via __getitem__ and __setitem__ the allowable keys are 
[-len...len-1] where negative numbers wrap around to the end of the
sequence. This also applies to slice notation. However insert()
interprets any negative indice as a prepend and any indice >= len as an
append. This _seems_ inconsistent to me. What's the rational or model
behind this?

Point the second: in a set of container classes I'm implementing, I have
some synonym methods, i.e. methods with different names that just call
the other implemented function. Ideally, these should share the same
docstring, but instead of just copying it, I thought it would be easy to
just put in:

class MyQueue:
   def push (self, item):
      """The docstring for push!"""
      # blah, blah, blah ...

   def enqueue (self, item):
      __class__.push.__doc__
      # just a synomym
      self.push (item)

... which didn't work.  Other possibilities included:

   enqueue.__doc__  = push.__doc__

... at the class level as effectively a static/class member. It's hardly
important but how would one do this, or are the docstrings put in place
by the compiler?

p-m

-- 
Paul-Michael Agapow (p.agapow at ic.ac.uk), Biology, Imperial College
"Pikachu's special power is that he is monophyletic with lagomorphs ..."



More information about the Python-list mailing list