osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Why is the interpreter is returning a 'reference'?


On 2/17/14 12:00 PM, Nir wrote:
>>>> k = ['hi','boss']
>>>>
>>>> k
> ['hi', 'boss']
>>>> k= [s.upper for s in k]
>>>> k
> [<built-in method upper of str object at 0x00000000021B2AF8>, <built-in method upper of str object at 0x0000000002283F58>]
>
> Why doesn't the python interpreter just return
> ['HI, 'BOSS'] ?
>
> This isn't a big deal, but I am just curious as to why it does this.
>

You have to invoke s.upper, with parens:

     k = [s.upper() for s in k]

In Python, a function or method is a first-class object, so "s.upper" is 
a reference to the method, "s.upper()" is the result of calling the method.

-- 
Ned Batchelder, http://nedbatchelder.com