subclassable types

Jean-Michel Pichavant jeanmichel at sequans.com
Fri Feb 22 04:51:03 EST 2013


----- Original Message -----
> 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!
> 
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
> 

Hi,

The BDFL explaining why you cannot subclass bool:

http://mail.python.org/pipermail/python-dev/2002-March/020822.html

"A subclass would only be useful when
it has instances, but the mere existance of an instance of a subclass
of bool would break the invariant that True and False are the only
instances of bool!  (An instance of a subclass of C is also an
instance of C.)  I think it's important not to provide a backdoor to
create additional bool instances, so I think bool should not be
subclassable."

Note that range is a function, not a class, hence the error when inheriting from it.

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.


More information about the Python-list mailing list