Is there any way to have a property and a method with the same name?
I mean a property that can be used the usual way and to be callable at the same time?
Like this:
>>> b = Book()
>>> b.pages
123
>>> b.pages()
123
>>> b.pages(including_toc=False)
123
>>> b.pages(including_toc=True)
127
No, you can’t.
()always calls an object from the expression on its left-hand side.What this means is, that
b.pages()can be read as follows:As you can see, methods are attributes.
What you could (but shouldn’t) do is wrap integers in some custom class and provide a
__call__method… but I would advise against such black magic.