Reason why xrange is not inheritable in Python? [duplicate]
Asked Answered
M

1

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?

Mitchel answered 28/2, 2014 at 14:37 Comment(4)
Related: #16057074Woermer
FYI, you get similar error when trying subclass bool, slice or buffer.Partridgeberry
Also related: #10062252Cagey
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.Badman
C
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.

Cagey answered 28/2, 2014 at 14:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.