On Mon, Dec 28, 2020 at 7:45 PM Anton Abrosimov <abrosimov....@gmail.com> wrote:
>
> Steven D'Aprano wrote:
> > Why do you want something that isn't a mapping to be usable with mapping
> > unpacking?
>
> I think mapping is not `abc.Mapping` class only.
>
> What about:
> `Iterator[Tuple[str, int]]`
>
> ```
> @dataclass
> class MyMap:
>     x: int
>     y: int
> ```
>
> Is this "mapping"?
> In Python I can use `/` as path separator: `pathlib.Path.cwd() / 'my_dir'`. I 
> can control the behavior of my class.
> But I only have one way to unpack the object. Not a perfect way. `dict.update 
> ()` gives more freedom.
>
> Steven D'Aprano wrote:
> > Does it really hurt you to provide mapping methods when you get them
> > for free? Just inherit from Mapping.
>
> ```
> # first.py:
> @dataclass
> class Point2D(Mapping):
>
> # second.py
> @dataclass
> class Point3D(Point2D):
> ```
>
> Now I have to think about unnecessary public methods.

Allow me to rephrase what I *think* you're arguing here, and you can
tell me if I'm close to the mark.

Given an object of a custom class C, you can make it usable as "x, y,
z = C()" or "f(*C())" or anything else by defining __iter__, and in
all ways that object will be iterable, unpackable, etc.

Given the same object, how can you ensure that it can be used as
"f(**C())"? What about in "{}.update(C())"? Or "dict(C())"? Is there a
single well-defined protocol that allows you to make your object
usable in all mapping-like contexts?

If that's not what your point is, ignore this post :)

ChrisA
_______________________________________________
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/C75H7LYS3ZLF44ION2K5JYIII7PGQH2M/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to