PEP308: Yet another syntax proposal

Andrew Dalke adalke at mindspring.com
Mon Feb 10 19:43:08 CET 2003


Raymond Hettinger
> > #  Example where all three reasons apply
> > data = isinstance(source, str)  ??   source.readlines()  ||
> source.split()
>
> That should have been:
>           isinstance(source, file)

Err, that should have been

    if hasattr(source, "readlines"):
      lines = source.readlines()
    else:
      lines = source.split()

Or perhaps better as
    try:
        lines = source.readlines()
    except AttributeError;
        lines = source.split()

Or for cases where you strongly prefer a file but will accept
a raw string (which you shouldn't do since StringIO is so easy
to get)

  if isinstance(source, basestring):
    source = StringIO(source)
  ...

and then you can change the alorithm to use read, readlines,
etc. as need be.

                    Andrew
                    dalke at dalkescientific.com







More information about the Python-list mailing list