join vs instances

Danyel Fisher danyelf at acm.org
Sun Dec 9 17:38:36 EST 2001


> I'd be curious to know when this (.data vs. builtin) matters. You'll
> either be writing code in the scope in which the UserString was
> instianted (which means, you'll know when to use .data and when not
> to), or perhaps, writing a function which requires a string arg, in
> which case you still don't have to care. You just call that func with
> foo.data if the string arg is a UserString.

But Robin could be calling a function which takes an argument, and
carries it around. Because UserString supports things like '+', there's
reason to let it slip:

def process1( arg ):
    # works
    return arg + "truth"

def process2( arg ):
    # breaks
    return ''.join( [arg, "truth" ]

a[1] = 'Foo'
a[2] = UserString( 'bar' )
z1 = map( process1, a  )
z2 = map( process2, a  )

> Of course it's expected that you can't get away with simply referring to
> the instance directly and expecting a string to pop out, but can you
> give an example of a gotcha with a UserString vs. builtin (that is to say,
> a scenario where you the difference between the two will make it
> impossible for you to write code that won't break)?

It wouldn't be impossible to tell, but it would be annoying to have to test.

Danyel





More information about the Python-list mailing list