[Tutor] capitalize() but only first letter

Erik Price erikprice@mac.com
Sun Feb 9 18:27:16 2003


On Friday, February 7, 2003, at 03:39  PM, Magnus Lycka wrote:

> At 10:43 2003-02-07 -0500, Erik Price wrote:
>> I'm not sure I understand why this works.
>
> Why not? The world doesn't HAVE to be complicated just
> because most other programming languages are! In Python
> almost everything is a first class object.

[...]

> Could hypothetically be something like this... (I'm sure it's not
> implemented anything like this really, Fredrik Lundh wrote it in C...)
>
> def sub(pattern, replacement, text):
>   ...
>   import types
>   while .... :
>       match = ...
>       beforeMatch = ...
>       afterMatch = ...
>       if type(replacement) == types.FunctionType:
>           text = beforeMatch+replacement(match)+afterMAtch
>       else:
>           text = beforeMatch+replacement+afterMatch
>       ...
>    return text


Sorry, I should have been more clear when I said "I'm not sure how this 
works" -- I wasn't taking issue with the fact that Python lets just 
about everything be an object, simply curious about how the re.sub() 
function determined whether it should act upon a string or use a 
function.  Your explanation makes it completely clear -- there's an 
internal type check that decides which should be done based on what the 
argument is.

Thanks!


Erik




-- 
Erik Price

email: erikprice@mac.com
jabber: erikprice@jabber.org