On Mon, Dec 28, 2020 at 09:06:40AM -0000, Anton Abrosimov wrote:

> Steven D'Aprano wrote:
> > You contradict yourself:
> > "I can implement any behaviour"
> > "I can't realize any other behaviour ..."
> > Which is correct?
> 
> I apologize for my english, I meant that I cannot implement the 
> following behavior inside the class:
> 
> ```
> class MyClass:
>     def __iter__(self):
>         return self.items_for_iteration
>     def __unpack__(self):
>         return self.items_for_unpack
> ```
> I have to make a separate method and have to rely on the user of the class.

Ah, now I understand what you mean: you want iteration and iterator 
unpacking to do different things:

    obj = MyClass()
    list(obj)  # iteration
    # --> returns a b c d
    print(*obj)  # iterator unpacking
    # --> returns x y z


You can't do that, just like you can't make these different:

    items = list(obj)  # iteration
    
    items = [item for item in obj]  # iteration in a comprehension

    items = []
    for item in obj:  # iteration in a for-loop
        items.append(item)

And that is a **good thing** because it would be confusing and horrible 
if iteration over an object was different depending on how you iterate 
over it.

We're not going to invent new dunder methods:

    def __iter__(self):

    def __forloop__(self):

    def __comprehension__(self):

so that they can be different, and I don't think we should invent a new 
dunder method `__unpack__` so it can be different from iteration.

Iterator unpacking is just a form of iteration.



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

Reply via email to