Loop and the list

Peter Otten __peter__ at web.de
Fri May 14 12:24:42 EDT 2004


Krzysztof Szynter wrote:

> Peter Otten <__peter__ at web.de> wrote in news:c82q4f$120$06$1 at news.t-
> online.com:
> 
>> It just works.
> 
> Ok. I got it. But why there is no difference between max and max()? I
> thought both are independent. I treat 'max' like a variable and 'max()'
> like a function, so they can live independently. Even i gave 'max' a value
> (integer value for example).
> 
> Am i wrong? Or i didn't get it yet?
> 

There is no conceptual difference between functions and objects. You can
both bind them ad libitum:

>>> alpha = max
>>> max = 123
>>> max
123
>>> alpha                
<built-in function max>  
>>> alpha(1,2,3)
3
>>>

That a function remembers its original name is only a convenience for the
user. Functions are just objects that implement a () operator aka "callable
objects". Here's a (simplified) callable integer:

>>> class Int(int):
...     def __call__(self):
...             print "I'm a callable integer. Weird, n'est-ce pas?"
...
>>> i = Int()
>>> i
0
>>> i()
I'm a callable integer. Weird, n'est-ce pas?
>>>

Peter




More information about the Python-list mailing list