0

I want to disable __iter__ in subclass of tuple. I do it like this:

def __iter__(self):
    raise TypeError("'%s' object is not iterable" % self.__class__.__name__)

But it seems not the right way. Anyone can help?

wtayyeb
  • 1,879
  • 2
  • 18
  • 38
  • 6
    [Barbara, are you there to answer this?](http://en.wikipedia.org/wiki/Liskov_substitution_principle) – Joachim Isaksson Aug 01 '13 at 13:32
  • 3
    Why in *heavens* name do you need this? – Martijn Pieters Aug 01 '13 at 13:35
  • Please don't. But look at this http://stackoverflow.com/questions/1684099/is-it-possible-to-delete-a-method-from-an-object-not-class-in-python – doctorlove Aug 01 '13 at 13:35
  • Why would you want to do this?... it will break expectations and code fairly quickly. – Jon Clements Aug 01 '13 at 13:36
  • just want not be able to iterate. I have problem with `'%G' % myinstance`. the subclass have a magic class `__float__` but it doesn't use it. – wtayyeb Aug 01 '13 at 13:39
  • 3
    @martijn the most appropriate would be NotImplementedError - then at least a programmer can ask "wtf not?" - instead of thinking a debug mission is in order – Jon Clements Aug 01 '13 at 13:46
  • @JonClements: Yeah, that one is probably better. – Martijn Pieters Aug 01 '13 at 13:51
  • 2
    Because you have a *container*. Why do you have a subclass of tuple that pretends to be a floating point value? Is formatting the only problem? Then use `__format__()` instead. – Martijn Pieters Aug 01 '13 at 13:52
  • 1
    Please tell us what you *really* try to do here, and avoid [X-Y problems](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Martijn Pieters Aug 01 '13 at 13:53
  • 1
    Although it a very questionable thing to do, object-oriented-programming-wise, yes that's the way to do it -- although you might want to raise a more descriptive error type like `NotImplementedError` as Jon suggested. `RuntimeError`, or even derive your own: i.e. `class InexplicablyNotIterableError(Exception)`. – martineau Aug 01 '13 at 15:21

0 Answers0