Search code examples
pythonpython-3.7pep8

Use of None and self keywords in method construction


I'm analyzing some old code that I've inherited, and I have a question about the use of "self" and "None" keywords, specifically in the following example:

def run(self) -> None:

I understand that the self keyword is similar to the "this" keyword in C++ in that, in conjunction with the dot operator, it allows us to access the attributes and methods of the class in question. What I'm really interested in is the use of "-> None" in the declaration of the method named "run." Is this in PEP 8 because I can't find an example. I'm using Python 3.7, in case that matters.

What is the purpose of writing a method in this manner? What does "-> None" do?


Solution

  • They're called type hints, and they enable annotating the types of the parameters and return types of functions.

    https://peps.python.org/pep-0484/