views:

72

answers:

1

Here's the code: http://paste.pocoo.org/show/238093/

My main questions right now are:

  1. Is Line 37 mainly the gist of this program? And does it simply calculate this once and then print the result? Ex: self.start + key*self.step with start=1, key=4, step=2 [prints 9]

  2. where does the variable 'value' actually come into play here? Line 39.

  3. Not worried about the "Exceptions" part of the program. I pretty much understand what it's doing.

  4. Lastly, and you really don't have to answer this one as it's probably better as another question "down the road" but I really do not see how __getitem__, __setitem__...etc...you still have to write in your own code to "make it do stuff". :) I'm just not getting what's so "special" about these special methods.

+3  A: 
  1. Yes, more or less.
  2. This is the exception. If someone assigns a value to a particular index, the sequence remembers that and will return that value instead of calculating it. Note that the code here does not actually use this function.
  3. Random comment instead: the last 3 lines of the getitem function could be much more concisely implemented as return self.changed.get(key, self.start + key*self.step) -- dict.get lets you provide a default to return if a key is missing.
  4. They're "special" only in that they let you override what happens when someone does yourthing[foo] or yourthing[foo] = bar. You see the first going on here; the second is what happens if someone does s[5] = 100 -- the 100 ends up as the value of a __setitem__ call.
Walter Mundt
Excellent comments/replies! I couldn't asked for anything better. I understand more of what is going on now and *now* I can move on to other topics. Thanks for your help, I really do appreciate it.
jimmyc3po
If it was helpful, toss an upvote his way, and mark the answer as accepted. That's the appropriate way to show your thanks on StackOverflow. (although the comment is probably appreciated too.)
Wilduck
Oh, okay. Thanks for letting me know. "First timer". :)
jimmyc3po