4

I was trying to inherit xrange to enable the target object behaviors of an integer list (iterable and in operator support). 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 of xrange?

Also, probably not related to that question, I noticed xrange has not method __contains__. For in operation, my basic knowledge is, a in A is equivalent to A.contains(a). Am I wrong, or xrange is something different?

I don't know if I should paste these as two separated questions. Appologize ahead.

Frozen Flame
  • 2,855
  • 2
  • 22
  • 33
  • 1
    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

1 Answers1

3

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
  • 5,939
  • 7
  • 30
  • 59