.title() - annoying mistake

Benjamin Schollnick bschollnick at schollnick.net
Mon Mar 22 06:47:12 EDT 2021


Robert,

I certainly see your point.  

> My only issue is that I completely fail to see how this function would be
> useful enough to warrant the inclusion into the *core* of a general-purpose
> language, including its misleading name.

But that’s where we have to disagree.

Sure, the name cold be a bit less “misleading”, I guess we could petition to have it renamed to:

def nonlanguage_aware_title		or 	def	title_but_not_language_aware

But I’m sorry for the majority of us, we haven’t even realized that title wasn’t language aware.   That has to tell you something (mainly that we’ve never hit these problems).  

Now that doesn’t diminish your issue here.  

But you’re a programmer, people have already suggested someway to work around this with RE.

The other python specific might be to actually SPLIT on the ` symbol, title each segment returning from split, and the JOIN them together again.

Regarding the issue of THE in an actual title, I’d suggest that RE would be the best solution for that, it should be easy enough to come up with RE that’ll work for word “xxx The xxx” and convert it to “xxx the xxx”.  

> The fact that the function's behavior
> is correctly documented doesn't make its very existence less bewildering to me.

Okay, I don’t recall you actually accepting at how large of a problem you are asking title to address.

The standard library is not going to fix and solve everything, like Apple I would expect them to target ~90% of the functionality, and then let people grow and expand on it’s functionality.  (Sort of like my CSV wrapper)

title is roughly comparable to PHP’s ucwords.

Wow, perl takes the prize though, it’s called ucwords.

$foo = 'hello world!';
$foo = ucwords($foo);             // Hello World!

$bar = 'HELLO WORLD!';
$bar = ucwords($bar);             // HELLO WORLD!
$bar = ucwords(strtolower($bar)); // Hello World!

perl appears to only manipulate the characters that it’s changing, so if the string is already uppercase, then it’ll just look like the entire string is uppercase when it’s done.  

So honestly that’s a mostly useless function if you need to pass a lowercase string into it.

What’s in common here?  both perl and php also seem to take the ` as a word separator.  

Neither of them appear to be language aware.

> Consider this function:
> 
> def add_seventeen(n): '''Return n with 16.8 added''' return n + 16.8
> 
> It's like .title(): It does almost the thing its name suggests, it is correctly
> documented, it is useful to anybody who happens to want 16.8 added to numbers,
> and it might erroneously be used by someone who wants exactly 17 added and
> didn't bother to read the docs.

And would be laughed at significantly.  

I agree with your example, but I don’t consider it to be equivalent.  

title does at first, and second glance work as expected.  You have significantly higher expectations than we do, evidently.

At minimum, what I would suggest is come up with some test case examples, and toss that over to the python dev team.  Show them that this isn’t working as you expect, and see what they say.  

While there might be a few of the python developers on this list, I doubt it.  Instead bring this up in a productive manner.  Show the harm, and then show a solution.  A Proposed change, an alternative framework.

We can’t solve the problem, but if you present it properly, then maybe you can be part of the solution.

> 
>> And as I mentioned the sheer amount of work that would be needed would
>> probably cover a phd dissertation or more…  It’s a huge problem set to
>> respect one language, let alone all languages.  
> 
> And that's why I believe that such a function should be delegated to a natural
> language-processing library and not the core of Python.

I have to disagree.  While I might expect a better version in a natural language processing library, every programming language that I’m aware of except for BASIC, offers an equivalent to title, as long as they offer an equivalent to upper and lower.

Why should python not offer title in light of this?

> said, I doubt that .title() would make it into Python today if it weren't there
> already. I'm having fun with this.

Ah, so while being a bit serious, I’m reading a bit too much into this.  

At this point, it’s become an interesting thought experiment for you.  

Good luck,

		- Benjamin




More information about the Python-list mailing list