Sujet : Re: Is Lexical Binding The Norm Yet?
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.lisp comp.lang.pythonDate : 20. Jan 2024, 11:13:40
Autres entêtes
Organisation : Stefan Ram
Message-ID : <prototype-20240120111210@ram.dialup.fu-berlin.de>
References : 1 2 3 4 5 6 7 8 9 10
Newsgroups: comp.lang.lisp,comp.lang.python
Lawrence D'Oliveiro <
ldo@nz.invalid> writes:
On Thu, 18 Jan 2024 23:39:26 -0500, George Neuner wrote:
Programming with closures is more like using "prototype OO". Prototype
systems don't have classes, but rather ANY object can be modified to
change its set of instance data and/or methods, and can be cloned to
create new objects of that same "type".
That’s true of Python, too.
Yes that's true. Forgive me guys if that's too "off topic"
in comp.lang.lisp, but it might not be obvious how to create
an object in Python and then attach fields or methods to it.
So here I humbly submit a small example program to show this.
main.py
from types import *
# create a new object
def counter_object(): pass
# attach a numeric field to the object
counter_object.counter_value = 0
# define a named function
def increment_value( self ): self.counter_value += 1
# attach the named function to the object as a method
counter_object.increment_value = \
MethodType( increment_value, counter_object )
# call the method
counter_object.increment_value()
# attach an unnamed function to the object
counter_object.get_value = \
MethodType( lambda self: self.counter_value, counter_object )
# call that method
print( counter_object.get_value() )
This program will then print "1" and a line terminator.
Newsgroups: comp.lang.lisp,comp.lang.python