interactive help on the base object

Chris Angelico rosuav at gmail.com
Mon Dec 9 03:44:01 CET 2013


On Mon, Dec 9, 2013 at 10:01 AM, Mark Janssen <dreamingforward at gmail.com> wrote:
> (Note bene: as a comparison, C++ is very UNAMBIGUOUS about
> this fact -- all objects inherit from concrete machine types, which is
> why it remains important, *despite* being one of the worst to do OOP
> in.  Its *type model* is probably the most clear of any
> object-oriented language).

Factually wrong. In C++, it is actually *impossible* to inherit from a
"concrete machine type", by which presumably you mean the classic
types int/char/float etc.

struct foo: public int
{
        foo() {}
};

1.cpp:1:20: error: expected class-name before ‘int’
1.cpp:1:20: error: expected ‘{’ before ‘int’
1.cpp:2:1: error: expected unqualified-id before ‘{’ token

Okay, that's a parse error. Maybe if we avoid the language keyword?

typedef int integer;

struct foo: public integer
{
        foo() {}
};

1.cpp:4:1: error: expected class-name before ‘{’ token

Nope. There are two completely different groups here: the basic types
(called here [1] "Fundamental data types") and the structure types.
The latter are created by the struct/class keyword and can use
inheritance. The former... aren't. Not every C++ type is part of the
type hierarchy.

This is actually somewhat true of Python, too, but the set of types
that are unavailable for inheritance is much smaller and less useful.
You can't inherit from the 'function' type, for instance:

>>> class foo(type(lambda:1)):
pass

Traceback (most recent call last):
  File "<pyshell#80>", line 1, in <module>
    class foo(type(lambda:1)):
TypeError: type 'function' is not an acceptable base type

And yet <class 'function'> inherits from class <'object'>, so it's not
entirely outside in the way C++ int is. And I can apparently subclass
module, though not generator, and unsurprisingly NoneType can't be
inherited from. (Though you can instantiate it, and it's probably the
only class that will appear to have no return value from
instantiation.)

C++ has you *compose* structs/classes from primitives, but this is not
the same as inheritance. They're completely separate concepts.

ChrisA

[1] http://www.cplusplus.com/doc/tutorial/variables/



More information about the Python-list mailing list