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

Chris Angelico rosuav at gmail.com
Tue Jun 2 04:37:28 EDT 2015


On Tue, Jun 2, 2015 at 5:44 PM, Rustom Mody <rustompmody at gmail.com> wrote:
>> Plates and spoons and knives and forks are objects.
>> Cars and trucks and ships and planes are objects.
>> Books and shoes and maps and compasses are objects.
>> Buildings are objects.
>> And blueprints of buildings are objects too.
>
> You are using 'are' as if they are all the same 'are'. Are they?
> Consider the type int.
> It is supposed to model mathematical integers.
> ie 1 ∈ int (in some sense)
>
> Consider the generator
> def int2():
>   i = 0
>   yield i
>   while True:
>     yield i
>     yield -i
>
> 1 ∈ int2 [Not to mention 1 ∈ int2()]
>
> Consider range(10) (in python2 and 3)
> And finally [1,2,3,4]
>
> 1 ∈ all these
> Are the '∈'s here same?  Similar?

Yes, they are. Every one of them is asserting (or asking, depending on
your point of view) whether or not the instance to its left is a
member of the set to its right. The sets on the right are all
different, but the set membership operation is identical.

But even more so: The original collection of statements ("are
objects") all used the same RHS. In exactly the same senses, all of
those examples were testing for the same "objectness". (Although you
could argue that Steven's examples were talking about subclass
relationships rather than instance relationships, but really, an
instanceof relationship is a special case of subclass, where the class
on the left is defined by some form of object identity. Stating "All
books are objects" is a subclass relationship; stating "This book is
an object" is an instanceof relationship; both assertions use the same
meaning of "object(s)".)

All shoes are objects.
All ships are objects.
All lumps of sealing-wax are objects.
All cabbages are objects.
All kings are objects. (Some kings are also subjects, but not many.)

I can pick up a shoe, I can look at it, I can verify that it has
certain attributes common to all objects (location, mass, etc).
Superman can do the same with a ship (they're a bit big for me to pick
up). The actions you can perform on all objects are the attributes of
objects in general. Some subclasses of object may provide additional
actions (you can tip a king, for instance), but you can still do all
of these actions, so they're all the same type of object.

Note that not one iota of this has anything to do with Python's
documentation, its rules, its behaviour, or anything. Python follows
the natural expectations of our universe. The standard actions that
can be performed on Python objects are slightly different from the
standard actions that can be performed in Dungeons and Dragons, and
different again from the standard actions of the world we live in, but
they are no less applicable to all objects in that hierarchy:

* You can bind a name to an object (any object).
* You can pass an object as a function parameter.
* You can return an object from a function.
* You can reference objects in other objects' attributes.
* You can distinguish one object from another (that is, objects have
unique identities).

(In CPython, you can recognize an object by its GC header - an object
invariably has a reference count and so on. This is not a fundamental
attribute of objects, but it's a way to recognize them.)

Not everything that Python uses is an object. Syntax is not, itself,
manipulable; you can represent a syntax tree with either source code
(as a string) or an AST object, but syntax itself is not something you
can grasp. You can't assign "foo = def" in Python. But anything that
you can work with, in any way, is an object - and is an instance of
(some subclass of) the type 'object'. So, yes: They ARE all the same
meaning of "are" and "objects".

ChrisA



More information about the Python-list mailing list