The definition of an object in Python

Andrew Koenig ark at acm.org
Sun Jul 13 13:51:36 EDT 2003


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

Avi>   But when I do the following

Avi>       mystring = 'hello'

Avi>       print mystring.id()

Avi>       print mystring.type()

Avi>   Python complains that mystring does not possess the attributes
Avi>   id and type.

type and id are functions, not methods.

>>> mystring = 'hello'
>>> print mystring.id()         # using id as a method doesn't work
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'str' object has no attribute 'id'
>>> print id(mystring)          # using id as a function does
1456272
>>> print type(mystring)        # ditto for type
<type 'str'>


-- 
Andrew Koenig, ark at acm.org




More information about the Python-list mailing list