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.
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)
If the code above were to specifically check for `str`, `bytes`, or
`bytearray`, that would be more fragile. it would not accommodate any new
stringlike types in newer python versions, and there would be no way to
designate other custom classes as being logically scalar even though supporting
sequence behavior.
_______________________________________________
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/2VC23QL5OFIUGX4LAJ6AOMWJA7LN4VOX/
Code of Conduct: http://python.org/psf/codeofconduct/