Added a new syntax

Add a new syntax to return the function that returns None in the class to self

example:

class A():
    def __init__(self):
         pass

    def a(self) -> None:
        ...

    def b(self) -> None:
        ...

If you want to call a() and b() at the same time, you need to:

myclass = A()
myclass.a()
myclass.b()

My idea is to add a new identifier :: to let the function return self
example:

A().a()::b()

Here, the a function changes the None return value to self through ::

It’s not possible to use a syntax change at the calling location to change the returned value from the called function. The function either returns None or Self, based on how its code is written, regardless of how it is called.

At best, it’s subjective whether this is in any way an improvement over the explicit code you’d write now. I can’t imagine ever wanting to use the proposed syntax. In particular, you have no reference to the instance of A once a and b are called, which calls into question how (and why) the class A is defined in the first place.

Thanks for offering your suggestion. Changing the language is difficult, and only happens after a good deal of work. You need to provide real-world examples of things that are hard to do now, but will be easier to do if your idea is adopted. You need to consider the existing semantics of the language and whether your idea can be cleanly implemented.

Most ideas are not adopted, but the discussion can help shed light on how Python works, and the considerations in language design in general.

2 Likes

To extend that notion, I’ve moved this to Python Help.

1 Like

I’m not sure the whole thread should move, but ok.

I guess there isn’t a universal consensus on this, but that was the conclusion I reached from this thread about ideas.

This is called method chaining. With that as a term to search for, you should be able to find some previous discussions of similar features.