The definition of an object in Python

Discussion in 'Python' started by Avi Kak, Jul 13, 2003.

  1. Avi Kak

    Avi Kak Guest

    Hello:

    Please forgive me if my question is too silly or just not
    well-formed.

    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, not every object is a
    class instance.

    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.

    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.

    Avi Kak
    Avi Kak, Jul 13, 2003
    #1
    1. Advertising

  2. 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,
    Andrew Koenig, Jul 13, 2003
    #2
    1. Advertising

  3. Andrew Koenig <> wrote in message news:<>...
    > 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
    Michele Simionato, Jul 14, 2003
    #3
  4. (Avi Kak) wrote in message news:<>...
    > 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=
    Wesley J. Chun, Jul 19, 2003
    #4
  5. On 19 Jul 2003 01:52:56 -0700, (Wesley J. Chun) wrote:
    [...]
    >> 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.

    ^^^^^^^^^
    This should perhaps be emphasized. E.g., these results are not guaranteed,
    but they can happen:

    >>> idvalue = id(123)
    >>> idvalue, id(456)

    (7952152, 7952152)
    >>> idvalue == id(456)

    1

    I.e., the value of id(x) is not valid beyond the lifetime of x, which is
    obviously ok, but mistaken reuse of id-like numbers (e.g., such as you get from
    file('somefile').fileno()) is a traditional source of bugs, and I imagine
    naive use of id() could lead to similar ones.

    >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.


    Re (3): until, perhaps, you start taking about copying or pickling? ;-)

    Regards,
    Bengt Richter
    Bengt Richter, Jul 19, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jianli Shen
    Replies:
    1
    Views:
    580
    Victor Bazarov
    Mar 13, 2005
  2. Ark
    Replies:
    1
    Views:
    412
    Chris Torek
    Aug 7, 2004
  3. Jon Slaughter
    Replies:
    4
    Views:
    440
    Jon Slaughter
    Oct 26, 2005
  4. Pierre Yves
    Replies:
    2
    Views:
    481
    Pierre Yves
    Jan 10, 2008
  5. Steve Litt
    Replies:
    2
    Views:
    182
    Dejan Dimic
    Sep 29, 2008
Loading...

Share This Page