[Tutor] capitalize() but only first letter

Magnus Lycka magnus@thinkware.se
Fri Feb 7 15:59:01 2003


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. It's uniform
as no other language that I know. We often call this
quality orthogonality. The X and Y axis in a ordinary
coordinate system is orthogonal, which means that the
X value won't change just because you change the Y value
of a point and vice versa. This means that you can concern
yourself with one thing at a time. In a similar spirit, the
way we pass objects to functions doesn't depend on their
type in Python. Any object can be passed to a function.
(It's another issue whether that function can deal with
any object). Any object can be printed. Any object can
be compared with any other object and so on. It's very
regular. We quickly find our way in Python, because the
different parts of the language are so similar and regular.

Think about the sprintf, fprintf question earlier.

If you can format a string and write to a file, you
can write a formatted string to a file in Python. In
C you need to know a particular function for that
context.

It's as if Python was a city with streets going north
to south, and avenue going west to east. If houses are
addressed like "the corner of Formatting street and
Print Avenue", you need to know all the streets to find
any address. So if there are 100 streets and 100 avenues,
you need to be able to locate 200 things to find any
place in the city.

C is more like a city where every house has a name of
it's own... Just because you know the Print Formatting
house doesn't mean that you know where File Formatting
house is. It might be close... But you still have 10
000 house names to memorize.

In reality, there are much more than two dimensions in
prorgamming. On the other hand, not all intersections
are meaningful...

>As I understand it, you're saying:
>
>1. The re.sub() function accepts a string as its second argument

Yes.

>2. The re.sub() function will also accept a function name as its second 
>argument

Well, a function! Either we say "a string or a function",
or we say "the name of a string or the name of a function".
I don't know why you change perspectives like that. Strings
and functions are both objects, just as classes, instances,
modules and integers etc. Simple objects like strings and
numbers can be stated explicitly like "Hello" or 3.14, but
typically we pass objects by providing some kind of name or
reference. The reference might be the left hand side of an
assignment, as in "a = 5" or it might be a name from a definition
like "def f(x): return x*x" or "class A: pass" or it can be
the the return value from a function call etc. It's often
some kind of name...

>3. If the re.sub() function is passed a function name as its second 
>argument, it will call that function and pass it a match object argument.

Yes.

>   Is there some kind of internal type checking done to determine if the 
> argument is a string or a function name,

Yes, normal Python code can do that, and C as well.

>  or is this some esoteric feature of Python that I've never heard of at work?

No magic.

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



-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se