function attributes are like function objects

Roy Katz katz at Glue.umd.edu
Wed Jan 24 20:28:12 EST 2001


...in that they carry information which trancends their execution. 

Reading the 'function attributes' thread and looking at users' needs... I
have to ask... why function attributes?  why not function *objects*?
Granted! we have to instantiate function objects -- because we have no
true method for writing class functions.  I'm getting confused on 
the (narrow AFAIK) difference between classes and functions with
attributes. Consider!


def f():    # (1) functions with attributes!

 { 'data'=1, 'subfunction'=lambda: pass }   # proposed way to make attrs
 pass

- vs -      

class G:    # (2) classes with methods! 

 data = 1
 def subfunction(self): pass

g = G()

- usage -

f.data = 3;  f.subfunction()   # invokes attribute of f()
g.data = 3;  g.subfunction()   # invokes g.subfunction()



::

Presto! instant TMTOWTDI powder.  That **scares** me. 
The only substantial difference I see between the two methods (heh, I'm
overloading the word one to many times here) is that (2) passes self to
its instance attributes whereas (1) does not.   So at the risk of
having overlooked something substantial, I ask:   since we can implement
functors, what's the point of giving functions attributes?



IMHO -- maybe we could use the non-passing-self nature of 
functions-with-subfunction-attributes to find a way to finally do class
methods. 



/R


 






More information about the Python-list mailing list