Thank you Jonathan, This is indeed helpful, I have completely forgotten about these.
However, in my case, defaults are not callable defaults, but rather default values preset at a higher level object and it has little to do with function defaults. Although, I think it is possible to reshape the problem to use the mechanism you have provided. For example: class Node: def __init__(self, a=1): self.a = a class Container: def __init__(self, a): self.node_defaults = dict() self.node_cls = Node self.node_cls.__init__.__defaults__ = (a,) # The issue, however is that it modifies the constructor from outside # And it changes the Node class for the whole Runtime # copy, deepcopy do not work here either # Construct the node class programatically by inheritance leads to the same issue: # self.node_cls = type('Node2', (Node,), {}) # self.node_cls.__init__.__defaults__ = (a,) self.nodes = list() def new_node(self, *args, **kwds): node = self.node_cls(*args, **kwds) self.nodes.append(node) return node c = Container(a=2) print(c.new_node().a) # 2 print(c.new_node(3).a) # 3 print(Node().a) # 2 If you could provide an example, which solves the same issue that I have presented in a more elegant manner than simple dictionary storage, I would appreciate it. Kind regards, DG > On 18 Sep 2023, at 17:24, Jonathan Fine <jfine2...@gmail.com> wrote: > > Hi Dom > > In your original post you used your proposed addition to write code that > provides a way of handling defaults different from the standard mechanism, > and perhaps in your opinion better. > > The following example tells us something about defaults mechanism already > provided by Python: > > >>> def f(a, b=1, c=2): return a, b, c > ... > >>> f(0) > (0, 1, 2) > >>> f.__defaults__ > (1, 2) > >>> f.__defaults__ = (0, 1, 2) > >>> f() > (0, 1, 2) > >>> f.__defaults__ = (2, 1, 0) > >>> f() > (2, 1, 0) > > I am suspicious of your example in your original post because it does not > explicitly consider the possibilities already provided by Python for changing > default values on the fly. > > I hope this helps. > > Jonathan
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/MUGEZV47DCD7BO43JZ7AFFAGGKETWM5D/ Code of Conduct: http://python.org/psf/codeofconduct/