join vs instances

Brad Bollenbach bbollenbach at home.com
Sun Dec 9 11:11:27 EST 2001


In article <cw+m7LAfO0E8EwpC at jessikat.fsnet.co.uk>, Robin Becker wrote:
> I had expected to be able to use things like UserStrings interchangeably
> with ordinary strings, but 

You can. If you couldn't, UserString wouldn't serve much purpose.

>>>> from UserString import UserString
>>>> C=['a',UserString('B'),'c']
>>>> ''.join(C)
> Traceback (most recent call last):
>   File "<interactive input>", line 1, in ?
> TypeError: sequence item 1: expected string, instance found>

Keep in mind that when you call a class Foo as a function (as in x =
Foo()) it returns and /instance/ of said class. To get to the data in
the string itself, you have to do what the docs say:

    The instance's content is kept in a regular string or Unicode 
    string object, which is accessible via the data attribute of 
    UserString instances.

So:

mothra at mothra:~$ python2.1
Python 2.1.1 (#1, Nov 11 2001, 18:19:24) 
[GCC 2.95.4 20011006 (Debian prerelease)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> from UserString import UserString
>>> x = UserString('b')
>>> ''.join(['a', x.data, 'c'])
'abc'

Hope that helps,

Brad



More information about the Python-list mailing list