Implicit lists

Thomas Heller theller at python.net
Fri Jan 31 10:32:30 EST 2003


Christian Tismer <tismer at tismer.com> writes:

> Here the same, written in some bad Python style,
> but as a builtin, it would do quite much
> simplification for argument checking:
> 
>  >>> def iscontainer(obj):
> ... 	try:
> ... 		obj[0:0]   # is sequence
> ... 		try:
> ... 			buffer(obj)
> ... 			return False
> ... 		except:
> ... 			pass
> ... 		return True
> ... 	except:
> ... 		return False
> ...
>  >>> iscontainer("")
> 0
>  >>> iscontainer(1)
> 0
>  >>> iscontainer(())
> 1
> 
> I'm (ab)using the fact that sequences which are
> no containers usually support the buffer interface.

This is worse, IMO.

ctypes' instances support the buffer interface, whether
they are sequences (or containers) or not.

ctypes' sequence instances (Array, for example)
support indexing, but not slicing.

The latter point could be viewed as a bug, although I'm not sure.

---

All this speaks for Alex'

    try:
        obj+''
    except TypeError:
        return 

construct, although I can think of cases where it would
be problematic.  Think of huge 200 MB strings...

Thomas




More information about the Python-list mailing list