subclassable types

Discussion in 'Python' started by Wolfgang Maier, Feb 22, 2013.

  1. Dear all,
    I am wondering what the rules are that determine whether a built-in type is
    subclassable or not.
    As examples, why can you base your classes on int or set,
    but not on bool or range?
    Also: can you use introspection to find out whether a type is valid as a
    base type?
    Thanks for your help!
     
    Wolfgang Maier, Feb 22, 2013
    #1
    1. Advertising

  2. On Fri, 22 Feb 2013 09:35:25 +0000, Wolfgang Maier wrote:

    > Dear all,
    > I am wondering what the rules are that determine whether a built-in type
    > is subclassable or not.


    The type-designer thought it should, or shouldn't, be.


    > As examples, why can you base your classes on int or set, but not on
    > bool or range?


    In the case of bool, because of the rule that bool is a singleton
    (actually, "doubleton") class. Since a bool subclass would have
    instances, that would break the rule that there is only one true bool and
    one false bool.

    As for range, I'm not sure. It may not be a deliberate decision, but a
    side-effect of the implementation. I have a vague recollection of there
    being a difference between "heap types" and "non-heap types", which has
    probably got something to do with the C implementation.

    If that's the case, I see that both Jython and IronPython have treated
    this as a language restriction rather than an accident. IronPython
    especially is interesting:

    >>> class foo(xrange): # equivalent to range in Python3

    .... pass
    ....
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: cannot derive from IronPython.Runtime.XRange because it is
    sealed



    > Also: can you use introspection to find out whether a type is valid as a
    > base type?


    I don't believe so, but welcome correction. I would just try. If it
    succeeds, it will succeed, otherwise it will raise TypeError.



    --
    Steven
     
    Steven D'Aprano, Feb 22, 2013
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sven

    Re: subclassable types

    Sven, Feb 22, 2013, in forum: Python
    Replies:
    0
    Views:
    85
  2. Wolfgang Maier

    Re: subclassable types

    Wolfgang Maier, Feb 22, 2013, in forum: Python
    Replies:
    0
    Views:
    90
    Wolfgang Maier
    Feb 22, 2013
  3. Terry Reedy

    Re: subclassable types

    Terry Reedy, Feb 22, 2013, in forum: Python
    Replies:
    0
    Views:
    92
    Terry Reedy
    Feb 22, 2013
  4. Wolfgang Maier

    Re: subclassable types

    Wolfgang Maier, Feb 24, 2013, in forum: Python
    Replies:
    0
    Views:
    100
    Wolfgang Maier
    Feb 24, 2013
  5. Wolfgang Maier

    Re: subclassable types

    Wolfgang Maier, Feb 24, 2013, in forum: Python
    Replies:
    0
    Views:
    115
    Wolfgang Maier
    Feb 24, 2013
Loading...

Share This Page