Is 'everything' a refrence or isn't it?

Steven D'Aprano steve at REMOVETHIScyber.com.au
Sun Jan 15 02:08:59 EST 2006


On Sat, 14 Jan 2006 23:21:14 -0500, Mike Meyer wrote:

> Steven D'Aprano <steve at REMOVETHIScyber.com.au> writes:
>> Likewise instances of object() have a rich, object-oriented structure --
>> dir(object()) returns a list with twelve items -- but every instance is
>> identical.
> 
> That's because all the things in dir(object()) are also in dir(object):
> 
>>>> dir(object) == dir(object())
> True


You are correct, of course, but your "proof" that you are correct is
bogus. All that your experiment shows is that the list of strings returned
by dir() is identical for the instance object() and the class object. Now,
you and I are both perfectly capable of using our knowledge of Python to
draw the obvious conclusion, but that isn't necessarily true for all
class/instance pairs. Here is a simple example (no doubt you can think of
simpler, and more complex, examples):

class pseudo_object: 
    def parrot(self):
        return "Norwegian Blues have beautiful plumage."
    parrot = classmethod(parrot)
    def __init__(self):
        self.parrot = lambda: "Nobody expects the Spanish Inquisition!"

And yet dir() reports the same signature for pseudo_object class and
instances.

In fact, we could imagine some other implementation of Python, BogoPython
perhaps, where object instances didn't inherit their methods from the
object class. On initialization, a factory function populated each
instance with all the methods it needed, which shared name and
functionality with the methods in the class, but not code. Potentially,
each instance method could have a unique implementation.


> So those properties of object() are all identical because they're
> really properties of object. If you define an object() by those
> properties, you have to conclude that object is itself an instance of
> object().

In this specific case, object instances get their properties from the
class, but your conclusion doesn't follow. The easiest way to tell this is
to ask, can we distinguish the object class (type) from an object
instance? The answer is, yes we can:

>>> object
<type 'object'>
>>> object()
<object object at 0xf705d3b8>

Python can tell them apart, and so can we.



-- 
Steven.




More information about the Python-list mailing list