The definition of an object in Python
Michele Simionato
mis6 at pitt.edu
Mon Jul 14 15:40:01 EDT 2003
Andrew Koenig <ark at acm.org> wrote in message news:<yu991xwuiac7.fsf at tinker.research.att.com>...
> 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'>
Just for fun, to show that the built-ins "id" and "type" are regular objects:
>>> id(id)
1076547724
>>> type(type)
<type 'type'>
>>> type(id)
<type 'builtin_function_or_method'>
>>> id(type)
135272896
Michele
More information about the Python-list
mailing list