Re: [Python-Dev] replace on empty strings

2006-05-24 Thread Guido van Rossum
On 5/24/06, Fredrik Lundh [EMAIL PROTECTED] wrote:
 so, which one is correct ?

 Python 2.4.3
   .replace(, a)
 ''
   u.replace(u, ua)
 u'a'

Since 'x'.replace('', 'a') and u'x'.replace('', u'a') return 'axa' and
u'axa', respectively, I conclude that the unicode version is correct
and the 8-bit string version is an anomaly.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] replace on empty strings

2006-05-24 Thread Tim Peters
[/F]
 so, which one is correct ?

 Python 2.4.3
   .replace(, a)
 ''
   u.replace(u, ua)
 u'a'

[Greg Ewing]
 Probably there shouldn't be any correct in this case,
 i.e. the result of replacing an empty string should be
 undefined (because any string contains infinitely many
 empty substrings).

Where are they?  For a string s, I count s[0:0], s[1:1], ...,
s[len(s):len(s)], or len(s)+1 empty substrings in all.

While str and unicode `replace` currently disagree about that when
len(s)==0, they agree when len(s)0:

  .replace(, A)
'A A'
 u .replace(, A)
u'A A'

 +0 on raising an exception if you try.

I'd be +1, except the idea that there are len(s)+1 empty substrings in
a string is pretty much ubiquitous:

  in 
True
 u in u
True
 .index()
0
 u.index(u)
0
  .rindex()
1
 u .rindex(u)
1
 .count()
1
 u.count(u)
1
  .count()
2
 u .count(u)
2

So the current str.replace really is an oddball.
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] replace on empty strings

2006-05-24 Thread Guido van Rossum
On 5/24/06, Greg Ewing [EMAIL PROTECTED] wrote:
 Fredrik Lundh wrote:
  so, which one is correct ?
 
  Python 2.4.3
.replace(, a)
  ''
u.replace(u, ua)
  u'a'

 Probably there shouldn't be any correct in this case,
 i.e. the result of replacing an empty string should be
 undefined (because any string contains infinitely many
 empty substrings).

No. That's what older versions of Python did, and it was changed to
the current behavior, except someone screwed up the edge case for
8-bit strings.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com