Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why are some parameters of builtin functions prefixed with '__'?

I noticed that some builtins have paramenters prefixed with ‘__’.

For example repr with def repr(__obj) -> str as signature.

Is this only a naming convention for builtins or does it also have a functional purpose?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This has no effect on the runtime and is an old naming convention. Before Python had positional-only arguments, developers would mark arguments with double underscores to indicate that they should only be called by position, not by name. So, in the case of repr, the double underscore indicates that you should always write repr(whatever), never repr(__obj=whatever).

Nowadays, in modern Python, we would be more likely to write

def repr(obj, /): ...

But going back through and changing all standard library functions to do so isn’t a priority and would likely be considered a breaking change.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading