The definition of an object in Python

Wesley J. Chun wesc at deirdre.org
Sat Jul 19 04:52:56 EDT 2003


kak at purdue.edu (Avi Kak) wrote in message news:<94af9c5a.0307130919.1ab63400 at posting.google.com>...
> Hello:
> 
>   Please forgive me if my question is too silly or just not 
>   well-formed.

you're forgiven.  :-)  your question is well-formed *and* not silly.

 
>   Wesley Chun in his book (Core Python Programming) says that 
>   **everything** in Python is an object.  So I became curious
>   about the precise definition of an object in Python.  My
>   curiosity was also driven by Wesley's statement that while
>   every class instance is an object,

this is still true since, as you know, *everything* is an object. :-)


>   not every object is a class instance.

Python's treatment of objects is unique from other languages,
which makes this clause "true."  Python has a defined set of
object types which are NOT classes, general objects such as
integers, floats, strings, lists, dictionaries, files, classes, and
even types are objects.  a class instance is truly only an ob-
ject that has been instantiated from a user-defined class.

prior to Python 2.2, classes were "class" objects and instances
were "instance" objects.  as types and classes are starting to
merge starting in 2.2, classes are now "type" objects, and
instance's "type" is the class they have been instantiated from.

EXAMPLE (old-style classes):

>>> class C: pass
...
>>> c=C()
>>> type(C)
<type 'class'>
>>> type(c)
<type 'instance'>

EXAMPLE (new-style classes):

>>> class myInt(int): pass
... 
>>> i = myInt()
>>> type(i)
<class '__main__.myInt'>
>>> type(myInt)
<type 'type'>

 
>   Wesley says that every Python object must possess the following
>   three characteristics: 1) an identity, which can be retrieved 
>   by the function id(); 2) a type, which can be retrieved by
>   the function type(); and 3) a value.

again, this is also (still) true.  (1) the identity is what makes an
object unique from every other object currently in the interpreter.
some people view this as a "memory address" altho you wouldn't
use it as such since Python handles the memory management 4 u.
you are just guaranteed that any other object in the system will
NOT have the same id.  (2) the type of an object defines what its
characteristics (i.e., what methods and attributes it has, etc.) are,
and (3) the value goes without saying.

 
>   But when I do the following
> 
>       mystring = 'hello'
>       print mystring.id()
>       print mystring.type()
> 
>   Python complains that mystring does not possess the attributes
>   id and type.
> 
>   So now I am confused.  Any help with the resolution of this 
>   issue would be much appreciated.

as others have pointed out, id() and type() are built-in FUNCTIONs
and not general built-in METHODs.  each object type has its own
set of methods, and all behavior of an object and its attributes are
defined by its TYPE.  you can use the dir() built-in function to see
what attributes (data and methods) an object has.

hope this helps!

-wesley

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

"Core Python Programming", Prentice Hall PTR, (c) 2001
    http://starship.python.net/crew/wesc/cpp/

Silicon Valley-San Francisco Bay Area Python Users Group (BayPIGgies)
    http://www.baypiggies.net

wesley.j.chun :: wesc at deirdre.org
cyberweb.consulting :: cyberweb_consulting at yahoo.com
http://roadkill.com/~wesc/cyberweb/

E-Mail using vi(m)/emacs with M$ outlook:
    http://groups.google.com/groups?threadm=fcd9edc1.0208281104.752da4bd%40posting.google.com




More information about the Python-list mailing list