Hi Jonathan,

I wasn’t writing anything specialised on handling of defaults. My initial 
example pretty much covers it.

What defaults handler of python are you talking about?

This proposal is more about convenient refactoring and keeping things nicely 
synchronised where variable name and string value are preferred to be kept in 
sync. The default handling is just one such case. It can also be useful in 
logging.

I can make my initial example a bit more clear and closer to what actually 
happened:

class Container:
    def __init__(self, a):
        self.node_defaults = dict()
        self.node_defaults[f'{=a}'] = a
        self.nodes = list()

    def new_node(self, a=None):
        if a is None:
            a = self.node_defaults.get(f'{=a}')
        node = Node(a)
        self.nodes.append(a)
        return node


class Node:
    def __init__(self, a):
        self.a = a
Currently, I just store keys as close to the object that finally uses the value 
as possible. So to achieve similar overall effect. And I am not saying that the 
above is better than below. IMO, it all depends on individual case. My point is 
that it might be nice to have. And I think the reasoning for such thing goes 
more along the lines of convenience, rather than if similar result can be 
achieved in different ways.
class Container:
    def __init__(self, a):
        self.node_defaults = dict()
        self.node_defaults[Node.K_A] = a
        self.nodes = list()

    def new_node(self, a=None):
        if a is None:
            a = self.node_defaults.get(Node.K_A)
        node = Node(a)
        self.nodes.append(a)
        return node


class Node:
    K_A = 'a'

    def __init__(self, a):
        self.a = a

Regards,
DG

—What can not be solved by force, can be solved by deferred evaluation —


> On 16 Sep 2023, at 21:27, Jonathan Fine <jfine2...@gmail.com> wrote:
> 
> Hi Dom
> 
> In your original post you said you're writing some code to improve handling 
> of defaults (perhaps only in some situations). You also said that the feature 
> you're suggesting would make your improved default handler easier to write.
> 
> Python already handles default values at least fairly well in many 
> situations. Please would you provide at least one example of how your 
> proposed default handler would be better than that already provided by Python.
> 
> Please don't worry about the implementation. Please focus on the syntax and 
> semantics and comparison with what Python does.
> 
> I will value your response to my request more highly if it shows that you 
> have a clear understanding of Python's present behaviour. I'd also welcome 
> any example you provide of weaknesses in Python's present behaviour.
> 
> 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/JNDAOK6EPYAUJZA5DDG6R2S3AAKOY62J/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to