On Oct 13, 2019, at 14:42, Steve Jorgensen <[email protected]> wrote: > > It is usually quite meaningful to drill down into other kinds of immutable > collections. A data structure consisting of a hierarchy of tuples would be a > good example of that.
Sure, sometimes it makes sense to represent a tree as a tuple of recursively tuples instead of creating a Node class or similar. But in that case, wouldn’t you want to drill down only into tuples, not all sequences? And in the cases where you _did_ want to drill down into all sequences except strings, wouldn’t you also want to drill down into iterables that weren’t sequences? If someone passes you an array or an instance of an old-style-sequence-api type or even an iterator, why not drill down into that? It seems like this would just be a way to make it easier to write fragile and unintuitive interfaces to data structures that you could just as easily write better interfaces for without needing any new features. > Besides the drill-down, cases, there are times when it is valuable to test > whether a collection or a "scalar" was received as an argument. That is to be > avoided more often than not, but there are times (such as when implementing a > DSL where it is useful enough to want to do. > > def __setattr__(self, value_or_vl): > # When value_or_vl is a non-string sequence, then it is VERY likely > # to be a tuple (which is immutable). > if isinstance(Sequence, value_or_vl) and not isinstance(Scalar, > value_or_vl): > value, label = value_or_vl > else: > value = value_or_vl > label = None > > self.values.add(value) > self.value_labels['value'] = label or self.value_labels.get(value) I’m not sure what this code is intended to do. First, __setattr__ is always called with a name as well as a value: `spam.eggs = 2` calls `type(span).__setattr__(spam, 'eggs', 2)`. What syntax are you envisioning that would call it with just spam and 2, and what would it be used to? Then, at the end, you add the value to a set of values, and then you replace the label for the name 'value' in a dict with either the passed-in label or the existing label assigned to the value, which will presumably never exist unless the value happens to be the string 'value'. What is this supposed to be doing? Also, why does it matter that tuples are immutable here? All you’re doing with any Sequence is unpacking it into two values—which works just as well with a 2-element list as a 2-element tuple, and fails just as badly for a 3-element immutable sequence as a mutable one. And is there a reason you want to treat non-sequence iterables of 2 elements as scalars here? And even ignoring all of that, whatever this is supposed to do, it seems like the only reason you’re checking for non-string Sequence is that it’s “VERY likely to be a tuple”, and therefore a good approximation of checking for a tuple? Why not just check for a tuple instead, which is exactly rather than approximately what you want, and a lot simpler? I can see the vague sense for why Scalar might be useful, but I can’t think of any practical examples. So I’m at least +0 if you can think of any examples that would work, and should be implemented this way, but I don’t think either of these qualifies. Maybe it would help to come up with something concrete rather than an abstract toy example? _______________________________________________ Python-ideas mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/[email protected]/message/3PT4UD6I6SVWSQBSZ5UJ4DF6KAHWKB5S/ Code of Conduct: http://python.org/psf/codeofconduct/
