The definition of an object in Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Avi Kak

    The definition of an object in Python

    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
    [email protected]
  • Andrew Koenig

    #2
    Re: The definition of an object in Python

    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.
    [color=blue][color=green][color=darkred]
    >>> mystring = 'hello'
    >>> print mystring.id() # using id as a method doesn't work[/color][/color][/color]
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    AttributeError: 'str' object has no attribute 'id'[color=blue][color=green][color=darkred]
    >>> print id(mystring) # using id as a function does[/color][/color][/color]
    1456272[color=blue][color=green][color=darkred]
    >>> print type(mystring) # ditto for type[/color][/color][/color]
    <type 'str'>


    --
    Andrew Koenig, [email protected]

    Comment

    • Michele Simionato

      #3
      Re: The definition of an object in Python

      Andrew Koenig <[email protected]> wrote in message news:<yu991xwui ac7.fsf@tinker. research.att.co m>...[color=blue]
      > 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.
      >[color=green][color=darkred]
      > >>> mystring = 'hello'
      > >>> print mystring.id() # using id as a method doesn't work[/color][/color]
      > Traceback (most recent call last):
      > File "<stdin>", line 1, in ?
      > AttributeError: 'str' object has no attribute 'id'[color=green][color=darkred]
      > >>> print id(mystring) # using id as a function does[/color][/color]
      > 1456272[color=green][color=darkred]
      > >>> print type(mystring) # ditto for type[/color][/color]
      > <type 'str'>[/color]

      Just for fun, to show that the built-ins "id" and "type" are regular objects:
      [color=blue][color=green][color=darkred]
      >>> id(id)[/color][/color][/color]
      1076547724[color=blue][color=green][color=darkred]
      >>> type(type)[/color][/color][/color]
      <type 'type'>[color=blue][color=green][color=darkred]
      >>> type(id)[/color][/color][/color]
      <type 'builtin_functi on_or_method'>[color=blue][color=green][color=darkred]
      >>> id(type)[/color][/color][/color]
      135272896


      Michele

      Comment

      • Wesley J. Chun

        #4
        Re: The definition of an object in Python

        [email protected] (Avi Kak) wrote in message news:<94af9c5a. 0307130919.1ab6 [email protected] ogle.com>...[color=blue]
        > Hello:
        >
        > Please forgive me if my question is too silly or just not
        > well-formed.[/color]

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

        [color=blue]
        > 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,[/color]

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

        [color=blue]
        > not every object is a class instance.[/color]

        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):
        [color=blue][color=green][color=darkred]
        >>> class C: pass[/color][/color][/color]
        ....[color=blue][color=green][color=darkred]
        >>> c=C()
        >>> type(C)[/color][/color][/color]
        <type 'class'>[color=blue][color=green][color=darkred]
        >>> type(c)[/color][/color][/color]
        <type 'instance'>

        EXAMPLE (new-style classes):
        [color=blue][color=green][color=darkred]
        >>> class myInt(int): pass[/color][/color][/color]
        ....[color=blue][color=green][color=darkred]
        >>> i = myInt()
        >>> type(i)[/color][/color][/color]
        <class '__main__.myInt '>[color=blue][color=green][color=darkred]
        >>> type(myInt)[/color][/color][/color]
        <type 'type'>

        [color=blue]
        > 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.[/color]

        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.

        [color=blue]
        > 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.[/color]

        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


        Silicon Valley-San Francisco Bay Area Python Users Group (BayPIGgies)


        wesley.j.chun :: wesc at deirdre.org
        cyberweb.consul ting :: cyberweb_consul ting at yahoo.com
        Trust MotorTrend for the best car reviews, news, car rankings, and much more. With more than 70 years of experience, we're your home for everything automotive


        E-Mail using vi(m)/emacs with M$ outlook:

        Comment

        • Bengt Richter

          #5
          Re: The definition of an object in Python

          On 19 Jul 2003 01:52:56 -0700, [email protected] g (Wesley J. Chun) wrote:
          [...][color=blue][color=green]
          >> 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.[/color]
          >
          >again, this is also (still) true. (1) the identity is what makes an
          >object unique from every other object currently in the interpreter.[/color]
          ^^^^^^^^^
          This should perhaps be emphasized. E.g., these results are not guaranteed,
          but they can happen:
          [color=blue][color=green][color=darkred]
          >>> idvalue = id(123)
          >>> idvalue, id(456)[/color][/color][/color]
          (7952152, 7952152)[color=blue][color=green][color=darkred]
          >>> idvalue == id(456)[/color][/color][/color]
          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.
          [color=blue]
          >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
          >characteristic s (i.e., what methods and attributes it has, etc.) are,
          >and (3) the value goes without saying.[/color]

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

          Regards,
          Bengt Richter

          Comment

          Working...