Unification of Methods and Functions

Antoon Pardon apardon at forel.vub.ac.be
Wed May 12 03:41:32 EDT 2004


Op 2004-05-11, Terry Reedy schreef <tjreedy at udel.edu>:
>
> "Antoon Pardon" <apardon at forel.vub.ac.be> wrote in message
> news:slrnca1d94.1i9.apardon at trout.vub.ac.be...
>> I don't see how this contradicts what I want to say. In both case you
> have
>> something of the form
>>
>>   obj = cls()
>>
>> And in both obj.method() is equivallent to cls.method(obj)
>
> I believe Timothy's point was that inst.meth() is more general than any
> specific clas.meth(inst) whenever there is more than one possible meaning
> of 'clas'.  In the following snippet, one can only replace 'animal.speak',
> without changing semantics, with 'animal.__class__.speak(animal)' and not
> with any specific versioon of clas.speak(animal).  If something cannot be
> substituted without changing meaning, in a particular context, then, in
> that context, it literally does not mean the same thing.

Fine it is more general, it is like a curried function. IMO this is just
a tangent that has litlle to do with my main point but if you really
want to be accurate, I don't mind its just illustrates how much python
is doing implicitly.

> class mammal:
>   def speak(self): print 'umf'
>
> class dog(mammal):
>   def speak(self): print 'arf'
>
> class cat(mammal):
>   def speak(self): print 'meow'
>
> for animal in [mammal(), dog(), cat()]: animal.speak()
>>>>
> umf
> arf
> meow
>>>>
>
> Terry J. Reedy

def curry(f):

  def f1(*h):

    def fh(*t):

      return apply(f , h + t)

    return fh

  return f1


class obj:
  pass


def mammal():

  def speak(self): print 'umf'

  self = obj()
  self.speak = curry(speak)(self)
  return self


def dog():

  def speak(self): print 'arf'

  self = obj()
  self.speak = curry(speak)(self)
  return self
  

def cat():

  def speak(self): print 'meow'

  self = obj()
  self.speak = curry(speak)(self)
  return self


for animal in [mammal(), dog(), cat()]: animal.speak()


>>>
 umf
 arf
 meow
>>>


-- 
Antoon Pardon



More information about the Python-list mailing list