Feature request: subclassing FunctionType [Was: Some language proposals]

Michael Hudson mwh at python.net
Fri Feb 27 14:24:37 CET 2004

michele.simionato at poste.it (Michele Simionato) writes:


> There is some merit on all those points of view. Speaking for myself,
> I would certainly use more closures if they were better supported by 
> the language. The fact that closures are read-only don't bothers me too 
> much, since I also think that when you want write access to a closure 
> you are really trying to fake an object, and you are better off using
> a real object. On the other hand, I am really bothered by the scoping
> rules. My first post on this mailing list some time ago was caused by
> problems I had with the surprising scope rules. Take for instance
> this example:
> def make_adders(n):
>     return [(lambda x: x+i) for i in range(n)]
> add0,add1=make_adders(2)
> print add0(0) #=> 1
> print add1(0) #=> 1
> This does not work as expected, 

However, you would probably like this to work:

def make_mutual_rec():
    def y1(x):
        if x > 0: return y2(x/2)
        else: return x
    def y2(x):
        if x > 0: return y2(x-2)
        else: return x
    return y1


It seems to be difficult to reconcile the two points of view.

More reading:


> and the solution is an ugly hack with default arguments:

Or an object...

> I don't like that, but I don't think that the current scope rules
> will change any soon. Alternatively, I can use a class:


> I think this reads quite well. At the present, however, I cannot subclass
> FunctionType and I am a bit disturbed by that, also because I can subclass 
> the other built-ins types and there no reason why functions must be so
> special: at they end they are just callable objects with a descriptor
> protocol. So, I submit it as a feature request for Python 2.4.
> Maybe, if enough people wants the feature, we may get it! 

I suspect that creating the patch wouldn't take much longer than
writing that article.


  In general, I'd recommend injecting LSD directly into your temples,
  Syd-Barret-style, before mucking with Motif's resource framework.
  The former has far lower odds of leading directly to terminal
  insanity.                                            -- Dan Martinez

More information about the Python-list mailing list