<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:large"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:large">I just discovered for myself what many must already know: lambda expressions may serve as dict keys (I knew values). That means they may partake of the mutability of some functions, in having mutable default parameter values. <br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:large"><br><br>In [122]: d = {lambda x=[10]: print(x.append(11), x) : 3}<br><br>In [123]: list(d.keys())[0]()<br><br>None [10, 11]<br><br><br>In [124]: list(d.keys())[0]()<br><br>None [10, 11, 11]<br><br><br>In [125]: list(d.keys())[0]()<br><br>None [10, 11, 11, 11]<br><br><br>In [126]: list(d.keys())[0]()<br><br>None [10, 11, 11, 11, 11]<br><br>That's not going to corrupt the dict though. In general, callable
objects may serve as keys, with lambda expressions a special case. So what if the object points to mutable content. <br><br><br>In [127]: class Callable:<br> ...: def __init__(self):<br> ...: self.the_list = []<br> ...: def __call__(self, *args):<br> ...: self.the_list.extend(args)<br><br><br>In [128]: obj = Callable()<br><br>In [129]: the_dict = {obj : 3}<br><br>In [130]: list(the_dict.keys())[0]("troll farm")<br><br>In [131]: obj.__dict__<br><br>Out[131]: {'the_list': ['troll farm']}<br><br>On the other hand, if you define __eq__ vis-a-vis something mutable,
then the dict-maker will have 2nd thoughts about the viability of your
enterprise.<br><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:large">So nothing that earth-shaking. It's the Python we all know.<br><br>Just thought I'd say hi. More soon.<br><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:large">Kirby Urner<br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:large">Portland, OR<br><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:large"><br></div></div>