Eric V. Smith wrote:
On 10/17/2019 2:36 PM, Steve Jorgensen wrote:
Andrew Barnert wrote: [snip] But meanwhile, you still haven’t given any reason why Record would be useful. What exactly is it supposed to mean? When would you want to check it? What would you do with an object only if you know it’s a Record? This isn’t like checking for a POD type in C++ or a struct type in Swift, because none of the important things that tells you even make sense in Python. The reason I am interested in having it is to disambiguate collections that are primarily to be treated as collections of values from those that are primarily to be accessed as attributes with values. As an example, when converting to JSON, it would make more sense to represent a regular tuple as a JSON array, but it would make more sense to represent a namedtuple as a JSON object. This seems specific to your application. I don't think the language can
disambiguate this for you. I think the best you can do is use the known way of looking for namedtuples and dataclasses. Although I think that in dataclasses case, is_dataclass is an anti-pattern and I should not have added it. For your use case, what's the difference between these two classes: @dataclass class A: x: int y: float class B: def __init__(self, x, y): self.x = x self.y = y Do you really want code that treats these differently, just because one used some shortcut to write the __init__ function? Eric
My thinking is that checking for type of `Struct` (or `Record` or whatever name it has) is most useful in cases when the object is also identifiable as a collection and is to disambiguate those that are primarily collections from those that are not.
A miscellaneous class like… def __init__(self, x, y): self.x = x self.y = y
…would not be a collection, so there would be no need to disambiguate it.