6

I was trying to inherit xrange (to implement membership testing with in, as well as iteration). But I got the following error message:

TypeError: Error when calling the metaclass bases
    type 'xrange' is not an acceptable base type

What's special about xrange?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Frozen Flame
  • 3,135
  • 2
  • 23
  • 35
  • 2
    Related: http://stackoverflow.com/questions/16056574/how-does-python-prevent-a-class-from-being-subclassed/ – Wooble Feb 28 '14 at 14:48
  • FYI, you get similar error when trying subclass `bool`, `slice` or `buffer`. – vartec Feb 28 '14 at 14:49
  • 1
    Also related: http://stackoverflow.com/questions/10061752/which-classes-cannot-be-subclassed – Bach Feb 28 '14 at 14:49
  • Note that the `range` type in Python 3.x already implements the behaviours that OP wanted - but is still not inheritable, so the question is not really version-specific. – Karl Knechtel Mar 22 '23 at 20:28

1 Answers1

5

xrange is implemented in C. As you can see in Tim Peters' post, there should be a convincing use case in order to justify the extra effort needed to allow subclassing of it.

Bach
  • 6,145
  • 7
  • 36
  • 61