{'id': <function Object.<locals>.id at 0x0000016547C648B0>, 'n': 2, [...]}c1 = Counter()
c1["inc"]()["inc"]()
2c1["n"]
{'id': <function Object.<locals>.id at 0x0000016547C64B80>, 'n': 0,c = Car()
c = Car()
c
{'id': <function Object.<locals>.id at 0x0000016547C64B80>, 'n': 1,c["inc"]()
0c["n"]
Is that at all possible somehow? Alternatively, how would you do your
toy oop-system?
Maybe something along those lines:
Meredith Montgomery <mmontgomery@levado.to> writes:
Is that at all possible somehow? Alternatively, how would you do your
toy oop-system?
Maybe something along those lines:
from functools import partial
def counter_create( object ):
object[ "n" ]= 0
def counter_increment( object ):
object[ "n" ]+= 1
def counter_value( object ):
return object[ "n" ]
counter_class =( counter_create, counter_increment, counter_value )
def inherit_from( class_, target ):
class_[ 0 ]( target )
for method in class_[ 1: ]:
target[ method.__name__ ]= partial( method, target )
car = dict()
inherit_from( counter_class, car )
print( car[ "counter_value" ]() )
car[ "counter_increment" ]()
print( car[ "counter_value" ]() )
. The "create" part is simplified. I just wanted to show how
to make methods like "counter_increment" act on the object
that inherited them using "partial".
Meredith Montgomery <mmontgomery@levado.to> writes:
Is that at all possible somehow? Alternatively, how would you do your
toy oop-system?
Maybe something along those lines:
from functools import partial
def counter_create( object ):
object[ "n" ]= 0
def counter_increment( object ):
object[ "n" ]+= 1
def counter_value( object ):
return object[ "n" ]
counter_class =( counter_create, counter_increment, counter_value )
def inherit_from( class_, target ):
class_[ 0 ]( target )
for method in class_[ 1: ]:
target[ method.__name__ ]= partial( method, target )
car = dict()
inherit_from( counter_class, car )
print( car[ "counter_value" ]() )
car[ "counter_increment" ]()
print( car[ "counter_value" ]() )
. The "create" part is simplified. I just wanted to show how
to make methods like "counter_increment" act on the object
that inherited them using "partial".
def Counter(name = None):
o = {"name": name if name else "untitled", "n": 0}
def inc(o):
o["n"] += 1
return o
o["inc"] = inc
def get(o):
return o["n"]
o["get"] = get
return o
On Sat, 24 Sept 2022 at 07:52, Meredith Montgomery
<mmontgomery@levado.to> wrote:
def Counter(name = None):
o = {"name": name if name else "untitled", "n": 0}
def inc(o):
o["n"] += 1
return o
o["inc"] = inc
def get(o):
return o["n"]
o["get"] = get
return o
Want a neat demo of how classes and closures are practically the same thing?
def Counter(name=None):
if not name: name = "untitled"
n = 0
def inc():
nonlocal n; n += 1
def get():
return n
return locals()
Aside from using a nonlocal declaration rather than "self.n", this is extremely similar to classes, yet there are no classes involved.
A class statement creates a namespace. The locals() function returns
the function's namespace.
Each call to Counter() creates a new closure context, just like each
call to a constructor creates a new object.
There's very little difference, at a fundamental level :)
On Sat, 24 Sept 2022 at 07:52, Meredith Montgomery<snip>
A class statement creates a namespace. The locals() function returns
the function's namespace.
Each call to Counter() creates a new closure context, just like each
call to a constructor creates a new object.
There's very little difference, at a fundamental level :)
The code below works, but you can see it's kinda ugly. I wish I could >uncurry a procedure, but I don't think this is possible. (Is it?)
ram@zedat.fu-berlin.de (Stefan Ram) writes:
Meredith Montgomery <mmontgomery@levado.to> writes:
Is that at all possible somehow? Alternatively, how would you do your >>>toy oop-system?
Maybe something along those lines:
from functools import partial
def counter_create( object ):
object[ "n" ]= 0
def counter_increment( object ):
object[ "n" ]+= 1
def counter_value( object ):
return object[ "n" ]
counter_class =( counter_create, counter_increment, counter_value )
def inherit_from( class_, target ):
class_[ 0 ]( target )
for method in class_[ 1: ]:
target[ method.__name__ ]= partial( method, target )
car = dict()
inherit_from( counter_class, car )
print( car[ "counter_value" ]() )
car[ "counter_increment" ]()
print( car[ "counter_value" ]() )
. The "create" part is simplified. I just wanted to show how
to make methods like "counter_increment" act on the object
that inherited them using "partial".
I really liked this idea. I organized it my way. Have a look. (Thank
you for the lecture!)
from functools import partial
def Counter(name = None):
o = {"name": name if name else "untitled", "n": 0}
def inc(o):
o["n"] += 1
return o
o["inc"] = inc
def get(o):
return o["n"]
o["get"] = get
return o
Ford: bruum!main()
Meredith Montgomery <mmont...@levado.to> writes:<snip>
r...@zedat.fu-berlin.de (Stefan Ram) writes:
. The "create" part is simplified. I just wanted to show how
to make methods like "counter_increment" act on the object
that inherited them using "partial".
I really liked this idea. I organized it my way. Have a look. (Thank
you for the lecture!)
But it lacks consistency.
Meredith Montgomery <mmontgomery@levado.to> writes:
The code below works, but you can see it's kinda ugly. I wish I could >>uncurry a procedure, but I don't think this is possible. (Is it?)
from functools import partial
from operator import add
add5 = partial( add, 5 )
print( add5( 2 ))
# might be dependent on implementation details of "functools":
uncurried = add5.func
print( uncurried( 14, 7 ))
However, to evaluate a method call such as "o.m( a, a1, ... )",
currying does not necessarily have to be used. One can as well
determine the function to be used for "m" from the type of "o"
and then call that function with arguments "o", "a", "a1", ...
ram@zedat.fu-berlin.de (Stefan Ram) writes:
Meredith Montgomery <mmontgomery@levado.to> writes:It works on my system here. Thank you so much for your help. (I've
The code below works, but you can see it's kinda ugly. I wish I could >>>uncurry a procedure, but I don't think this is possible. (Is it?)from functools import partial
from operator import add
add5 = partial( add, 5 )
print( add5( 2 ))
# might be dependent on implementation details of "functools":
uncurried = add5.func
print( uncurried( 14, 7 ))
been learning a lot with you!)
However, to evaluate a method call such as "o.m( a, a1, ... )",Was that your last approach?
currying does not necessarily have to be used. One can as well
determine the function to be used for "m" from the type of "o"
and then call that function with arguments "o", "a", "a1", ...
However, to evaluate a method call such as "o.m( a, a1, ... )",Was that your last approach?
currying does not necessarily have to be used. One can as well
determine the function to be used for "m" from the type of "o"
and then call that function with arguments "o", "a", "a1", ...
Yes, I think so.
(There are also languages with "multi-methods", where upon
a function call "m( o, o1, ... )" the decision which function
to call depends on all the types of all the arguments.)
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 300 |
Nodes: | 16 (2 / 14) |
Uptime: | 63:09:28 |
Calls: | 6,712 |
Files: | 12,244 |
Messages: | 5,355,973 |