[Tutor] Difference between object and instances

Gonçalo Rodrigues op73418 at mail.telepac.pt
Sun Mar 7 21:17:12 EST 2004

Em Mon, 8 Mar 2004 00:43:40 -0000, "Alan Gauld"
<alan.gauld at blueyonder.co.uk> atirou este peixe aos pinguins:

>> What is the real difference between objects and instances?
>The generally accepted terms are
>Class and Object where Object is an instanciation of a class.
>but in some languages, including Python things can be objects 
>which are not instances of classes - for example functions, 
>and even classes themselves!

Actually, in Python (*) -- I'm using 2.3 but this is true in 2.2
already -- functions and classes themselves are instances of a some

>>> def f(arg):
... 	pass
>>> f
<function f at 0x010D6B30>
>>> f.__class__
<type 'function'>
>>> id(f)
>>> import types
>>> isinstance(f, types.FunctionType)

As you can see it an instance of a class. You can't subclass
FunctionType although in c.l.py a request for it has appeared
recently. The same for classes

>>> F
<class '__main__.F'>
>>> id(F)
>>> F.__class__
<type 'type'>
>>> isinstance(F, type)

You *can* subclass the class type. And then we are in metaclass
programming lala-land.

In fact, ATM I can't recall any object in Python that's not an
instance of some class -- does anybody have a counterexample?

With my best regards,
G. Rodrigues

(*) This is not true in Java for example.

More information about the Tutor mailing list