function attributes are like function objects

Roy Katz katz at Glue.umd.edu
Thu Feb 1 04:46:41 CET 2001


Oh, good, perhaps a calll for a Py3k suggestion?

idea:

Perhaps replace def/class with a general 'def' which would
allow the definition of 
 
   - a standard class
   - a function
   - an instance method
   - a class method
   - a singleton class

all through a single, consistent syntax. 
lists, strings, dicts, ints and other types would be subclass-able. 

This would solve the following current problems:

   - Python lacks class methods 
   - Current Python has no support for subclassing from primitive
     types. 
    
I really would like that -- provided it is consistent and not
occam's-razor syntax extension. 

I had an idea where one could write

  def f(x):   pass
  def C:
    def f(self):  pass
    def g():      pass  # where some new syntax indicates that g is static

    print 'hi'          # C is like a function and a class in one. 

and you could call f(3), C.g(), C() to call C as a function.. 
that'd fill those requirements... sort of. 


On the other hand, maybe there needs to be a distinct separation between
class and function;  I think right now that function attributes are a
waste when the functions can use (1) global values,
(2) 'call-by-reference' (er, mutable, right?) object parameters in which
to hold values, or (3) be implemented as function objects, with all being
viable alternatives.  


(1)  

    state = 123

    def f(): 
      return state

(2) 

     class stateHolder:
       state = 0
       def __init__(self, state): self.state = state

     state = stateHolder( 123 )

     def f( s )    # s is a stateHolder
        return s.state 


(3) 

     class func:
        state = 123
        def f(self):
            return self.state

-------


there.  I prefer (3), (I'm CS-oriented :))


Roey
 



On Thu, 1 Feb 2001, Magnus Lie Hetland wrote:

> "Greg Ewing" <greg at cosc.canterbury.ac.nz> wrote in message
> news:3A78AE8D.C9D25694 at cosc.canterbury.ac.nz...
> > Sean Reifschneider wrote:
> > >
> > > It didn't really
> > > seem like they were missing so much as it seemed like they would act
> > > more like everything else now...
> >
> > Everything? What about lists, dicts, tuples, file
> > objects...?
> >
> > What's so special about functions that we need to
> > be able to plonk arbitrary attributes on them, but
> > not any other builtin types?
> 
> Now *that* would be nice (if we could do that, I mean).
> Everything as objects... SmallTalk lurking in the
> background. Sounds good to me...
> 
> --
> 
>   Magnus Lie Hetland      (magnus at hetland dot org)
> 
>  "Reality is what refuses to disappear when you stop
>   believing in it"                 -- Philip K. Dick
> 
> 
> 
> 




More information about the Python-list mailing list