2.2 open

Bengt Richter bokr at oz.net
Sun Mar 24 21:09:16 EST 2002


On Sun, 24 Mar 2002 18:53:41 -0500, Tim Peters <tim.one at comcast.net> wrote:

>[Martin v. Loewis]
>> Correction: open is file. Just try it
>>
>> >>> open is file
>> 1
>>
>> So open is the type of file objects.
>
>Na, file is the type of file objects:
>
>>>> type(sys.stdout)
><type 'file'>
>>>> type(sys.stdout).__name__
>'file'
>
>"open" was made identical to "file" just so open(filename) would continue to
>work as before.  Once you get used to it, it's more natural to say
>file(filename) in 2.2 (for much the same reason it's long been more natural
>to say int("42") than string.atoi("42")).
>
 >>> id(open),id(file),open==file
 (503978992, 503978992, 1)

Really identical, it seems.
BTW, I was playing around classifying things in __builtins__ according
to type(thing).__name__:

 >>> sys.version
 '2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)]'

 >>> d={}
 >>> for k,v in __builtins__.__dict__.items():
 ...     vn = type(v).__name__
 ...     if d.has_key(vn): d[vn].append(str(k))
 ...     else: d[vn] = [str(k)]
 ...
 >>> for k in d.keys(): print k
 ...
 instance
 builtin_function_or_method
 str
 int
 ellipsis
 NotImplementedType
 type
 class
 NoneType

 >>> def pl(alist,cols=None,width=78):
 ...     fmt= '%%%ss' % (cols and width//cols-1 or '',)
 ...     for i in xrange(len(alist)):
 ...         if not cols:
 ...             print alist[i]
 ...         else:
 ...             if not i%cols:  print
 ...             print fmt % alist[i],
 ...
 >>>
 >>> pl(d['instance'],3)  

                      help                   credits                 copyright
                   license
 >>> pl(d['str'],3)

                      quit                   __doc__                      exit
                  __name__
 >>> pl(d['int'],3)

                 __debug__
 >>> pl(d['ellipsis'],3)

                  Ellipsis
 >>> pl(d['NotImplementedType'],3)

            NotImplemented
 >>> pl(d['NoneType'],3)

                      None
 >>> pl(d['class'],4)

       RuntimeError        MemoryError      StopIteration       UnicodeError
        LookupError     ReferenceError          NameError        ImportError
         SystemExit          Exception      StandardError        SystemError
            IOError         IndexError     RuntimeWarning      SyntaxWarning
            Warning    ArithmeticError           KeyError   EnvironmentError
 DeprecationWarning FloatingPointError    OverflowWarning         ValueError
           EOFError           TabError        SyntaxError            OSError
   IndentationError     AssertionError          TypeError  KeyboardInterrupt
        UserWarning  ZeroDivisionError  UnboundLocalError NotImplementedError
     AttributeError      OverflowError       WindowsError
 >>>

... all the exceptions appear to be type 'class'. Are they slated to become object-based?

 >>> pl(d['builtin_function_or_method'],4)

               vars                pow            globals             divmod
              apply         isinstance                zip                hex
                chr         __import__              input                oct
               repr            hasattr            delattr            setattr
          raw_input               iter            compile             reload
              round                dir                cmp               hash
             xrange             reduce             coerce             intern
         issubclass             unichr                 id             locals
              slice                min           execfile            getattr
                abs                map             buffer                max
                len           callable               eval                ord
             filter              range

 >>> pl(d['type'],4)

              float            unicode               open              super
               long            complex               dict               type
              tuple               list                str           property
                int               file             object                  _
        classmethod       staticmethod
 >>>

... sort of interesting, FWIW. Has someone done a proper showinfo(x) utility to
do this kind of thing for various objects? Hard to believe not.

Regards,
Bengt Richter




More information about the Python-list mailing list