Everything is an object in python - object class and type class

Steven D'Aprano steve at pearwood.info
Tue Jun 2 13:00:50 EDT 2015


On Tue, 2 Jun 2015 10:49 pm, BartC wrote:

> On 02/06/2015 12:27, Steven D'Aprano wrote:
> 
>> "Object" has a general meaning in computer science and programming, it is
>> a compound data structure that is explicitly linked to a type which
>> provides functionality that operates on that data structure.
>>
>> In the programming language C, *no* values are objects. C has types
>> (int16, uint32, bool, and many more) but no objects.
> 
> The C Standard has hundreds of references to 'object'.

Are any of them objects in the sense of object oriented programming?

I'm pretty sure the answer to that is No. C is widely agreed to *not* be an
object oriented language, although obviously you can construct an OOP
system in C.

It is difficult to give a single, unconditional definition of "object" in
the object-oriented programming sense, because there are many subtle
variations. We can consume many hours in fruitless debates over an exact
formal definition of "object" in the sense of OOP, but I don't think that
is productive. But I think this should cover most of the common cases:


* An object-oriented programming language is one which provides objects 
  as a native data type;

* objects are structured entities which combine:

  - behaviour (methods)
  - state (data, value)
  - and identity (unique existence);

* the structure and behaviour of the object is inherited from a 
  class (blueprint or template), or a prototype (another object).


The important thing is that the language provides these as ready-to-use
features, complete with syntax and support from the compiler/interpreter. I
could write OOP-style code in C, or even assembly language, but only by
writing my own framework, then taking care to only write code within the
boundaries of that framework. Hence, C is not OOP: objects aren't built-in
to C, but have to be added.


> C objects are also linked to a type but it doesn't need to be explicit
> /in the run-time data/ because it's a static language. The compiler
> knows all the types because there are explicit annotations in the source
> code.

OOP can exist in both statically typed languages (such as Java, C++,
Objective C) and dynamically typed languages (such as Python, Javascript,
Ruby).


>> In the programming language Java, *some* values are objects, and some
>> values are not objects.
>>
>> In the programming language Python, *all* values are objects, in the
>> general sense. That is what we mean by "Everything is an object".
> 
> In a dynamically typed language such as Python, you need to be able to
> deal with values consistently whatever their type, simply because you
> can't tell what the types are from source code. (Not without a lot of
> work which Python doesn't attempt, although RPython might do so.) Example:
> 
>   print (a)
> 
> a might be the int 42, or a it might be a million-element list. So both
> are wrapped up as 'objects'.

Again, this is not relevant. Javascript is dynamically typed, but some
values are machine primitives and other values are objects. The interpreter
keeps track of this at runtime.

Java is similar, except it keeps track if the difference between boxed and
unboxed types at compile time.


> Java is statically typed which makes it possible to treat instance
> variables differently without needing to examine their types at runtime.
> If you define 'object' in a certain way (eg. as boxed, tagged data),
> then it follows that some values don't need to be objects.

I'm not sure what point you are trying to make here.



-- 
Steven




More information about the Python-list mailing list