Is a list an instance of a class?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kent Johnson

    Is a list an instance of a class?

    In the Python tutorial section 9.1, it says, "the word ``object'' in
    Python does not necessarily mean a class instance. Like C++ and
    Modula-3, and unlike Smalltalk, not all types in Python are classes: the
    basic built-in types like integers and lists are not, and even somewhat
    more exotic types like files aren't."

    Is this still correct or was it made obsolete by Python 2.2? Lists and
    files have __class__ attributes, at least:
    [color=blue][color=green][color=darkred]
    >>> [].__class__[/color][/color][/color]
    <type 'list'>[color=blue][color=green][color=darkred]
    >>> f = open('ThreadQue ue.py')
    >>> f.__class__[/color][/color][/color]
    <type 'file'>

    So do integers and floats, though you have to ask nicely for an int:[color=blue][color=green][color=darkred]
    >>> 1.__class__[/color][/color][/color]
    File "<stdin>", line 1
    1.__class__
    ^
    SyntaxError: invalid syntax[color=blue][color=green][color=darkred]
    >>> x=1
    >>> x.__class__[/color][/color][/color]
    <type 'int'>[color=blue][color=green][color=darkred]
    >>> 0.5.__class__[/color][/color][/color]
    <type 'float'>

    Kent
  • Dan Bishop

    #2
    Re: Is a list an instance of a class?

    Kent Johnson <kent3737@yahoo .com> wrote in message news:<4197c013$ 1_1@newspeer2.t ds.net>...[color=blue]
    > In the Python tutorial section 9.1, it says, "the word ``object'' in
    > Python does not necessarily mean a class instance. Like C++ and
    > Modula-3, and unlike Smalltalk, not all types in Python are classes: the
    > basic built-in types like integers and lists are not, and even somewhat
    > more exotic types like files aren't."
    >
    > Is this still correct or was it made obsolete by Python 2.2? Lists and
    > files have __class__ attributes, at least:[/color]

    For all practical purposes, it's obsolete. You can inherit from "non-classes" now.
    [color=blue][color=green][color=darkred]
    >>> class TallyMarks(int) :[/color][/color][/color]
    .... def __repr__(self):
    .... return 'I' * self
    ....[color=blue][color=green][color=darkred]
    >>> TallyMarks(12)[/color][/color][/color]
    IIIIIIIIIIII

    [color=blue][color=green][color=darkred]
    > >>> [].__class__[/color][/color]
    > <type 'list'>[color=green][color=darkred]
    > >>> f = open('ThreadQue ue.py')
    > >>> f.__class__[/color][/color]
    > <type 'file'>
    >
    > So do integers and floats, though you have to ask nicely for an int:[color=green][color=darkred]
    > >>> 1.__class__[/color][/color]
    > File "<stdin>", line 1
    > 1.__class__
    > ^
    > SyntaxError: invalid syntax[color=green][color=darkred]
    > >>> x=1
    > >>> x.__class__[/color][/color]
    > <type 'int'>[/color]

    You don't have to ask *that* nicely.
    [color=blue][color=green][color=darkred]
    >>> 1 .__class__ # notice the space[/color][/color][/color]
    <type 'int'>

    Comment

    • Steven Bethard

      #3
      int literals and __class__ (WAS: Is a list an instance of a class?)

      Dan Bishop <danb_83 <at> yahoo.com> writes:[color=blue]
      > You don't have to ask *that* nicely.
      >[color=green][color=darkred]
      > >>> 1 .__class__ # notice the space[/color][/color]
      > <type 'int'>[/color]

      Why does this work? Or, perhaps my real question is why *doesn't* it work
      without the space? Pointers to the appropriate point in the docs would be
      fine... I assume it's something about making parsing easier...?

      Thanks,

      Steve

      Comment

      • Erik Max Francis

        #4
        Re: int literals and __class__ (WAS: Is a list an instance of a class?)

        Steven Bethard wrote:
        [color=blue]
        > Why does this work? Or, perhaps my real question is why *doesn't* it work
        > without the space? Pointers to the appropriate point in the docs would be
        > fine... I assume it's something about making parsing easier...?[/color]

        It's because otherwise it's confusing it for a floating point literal.
        All you need to do is put a break so that the parser can't get confused.
        1 ._class__ or (1).__class__ will work just fine.

        --
        Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
        San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
        Love is the triumph of imagination over intelligence.
        -- H.L. Mencken

        Comment

        • Alex Martelli

          #5
          Re: int literals and __class__

          Steven Bethard <steven.bethard @gmail.com> wrote:
          [color=blue]
          > Dan Bishop <danb_83 <at> yahoo.com> writes:[color=green]
          > > You don't have to ask *that* nicely.
          > >[color=darkred]
          > > >>> 1 .__class__ # notice the space[/color]
          > > <type 'int'>[/color]
          >
          > Why does this work? Or, perhaps my real question is why *doesn't* it work
          > without the space? Pointers to the appropriate point in the docs would be
          > fine... I assume it's something about making parsing easier...?[/color]

          It's to make lexing more predictable: it's known as maximal-much lexing
          (without backtracking: backtracking in lexical analysis would really but
          really be bad). '1.' (no space) is a token (stands for a float
          literal). So, '1.__class__' is two tokens: float '1.' followed by
          identified '__class__'.


          Alex

          Comment

          • Steve Holden

            #6
            Re: int literals and __class__ (WAS: Is a list an instance of a class?)

            Steven Bethard wrote:
            [color=blue]
            > Dan Bishop <danb_83 <at> yahoo.com> writes:
            >[color=green]
            >>You don't have to ask *that* nicely.
            >>
            >>[color=darkred]
            >>>>>1 .__class__ # notice the space[/color]
            >>
            >><type 'int'>[/color]
            >
            >
            > Why does this work? Or, perhaps my real question is why *doesn't* it work
            > without the space? Pointers to the appropriate point in the docs would be
            > fine... I assume it's something about making parsing easier...?
            >
            > Thanks,
            >
            > Steve
            >[/color]
            Yup, it's because a dot immediately following an integer would be parsed
            (strictly "lexed", I suppose) as a floating point constant,m aking the
            __class__ a syntax error.

            regards
            Steve
            --


            Holden Web LLC +1 800 494 3119

            Comment

            • Russell Blau

              #7
              Re: int literals and __class__ (WAS: Is a list an instance of a class?)

              "Steve Holden" <steve@holdenwe b.com> wrote in message
              news:_qmmd.1065 9$nj.4248@laker ead01...[color=blue]
              > Steven Bethard wrote:
              >[color=green]
              > > Dan Bishop <danb_83 <at> yahoo.com> writes:
              > >[color=darkred]
              > >>>>>1 .__class__ # notice the space
              > >>
              > >><type 'int'>[/color]
              > >
              > > Why does this work? Or, perhaps my real question is why *doesn't* it[/color][/color]
              work[color=blue][color=green]
              > > without the space? Pointers to the appropriate point in the docs would[/color][/color]
              be[color=blue][color=green]
              > > fine... I assume it's something about making parsing easier...?
              > >[/color]
              > Yup, it's because a dot immediately following an integer would be parsed
              > (strictly "lexed", I suppose) as a floating point constant,m aking the
              > __class__ a syntax error.
              >
              > regards
              > Steve[/color]

              And, in case anyone was wondering (as I was), the following works, too,
              despite the fact that it looks incredibly odd:
              [color=blue][color=green][color=darkred]
              >>> print 1. .__class__[/color][/color][/color]
              <type 'float'>

              --
              I don't actually read my hotmail account, but you can replace hotmail with
              excite if you really want to reach me.


              Comment

              Working...