Implicit Type Conversion buffer -> string

Stephan Herschel stephan at proceryon.at
Tue Mar 27 06:53:37 EST 2001


Hi,

Have a look at this example about use of buffers. Can someone explain?

stephan at tilos:~ > python
Python 1.5.2 (#1, Jul 29 2000, 14:28:37)  [GCC 2.95.2 19991024
(release)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> a='abcdefghi'
>>> aB=buffer(a)
>>> b='ABCDEFGHI'
>>> bB=buffer(b)
>>> print aB
abcdefghi
>>> type(aB)
<type 'buffer'>
>>> print aB[1:-1]
bcdefgh
>>> type(aB[1:-1])
<type 'string'>
>>> print aB[0:len(aB)]
abcdefghi
>>> type(aB[0:len(aB)])
<type 'buffer'>
###############################################
this is interesting: when slicing a buffer from beginning to end no type
conversion is happening
###############################################
>>> print a+b
abcdefghiABCDEFGHI
>>> print aB+bB
abcdefghiABCDEFGHI
>>> type(aB+bB)
<type 'string'>
>>> print a+bB
Traceback (innermost last):
  File "<stdin>", line 1, in ?
TypeError: illegal argument type for built-in operation
###############################################
this is strange: type conversion is happening when concatenating 2
buffers,
but why is it leading to a type error when concatenating a string with a
buffer??
###############################################
   
regards,
stephan



More information about the Python-list mailing list