Def

Class (Python)

A class in Python is a blueprint for creating objects, defined with the class keyword.

This page covers Class in Python. For a language-agnostic introduction, see Class.

Defining a Class#

class Player:
    pass

pass is a placeholder that means “empty body”. By convention, class names use PascalCase: Player, OddsAndEvens, RockPaperScissors.

Adding a Constructor#

In Python, the constructor is a special method called __init__. It runs automatically when an object is created. The first parameter is always self — a reference to the object being created.

Method (Python)

A method in Python is a function defined inside a class using the def keyword.

This page covers Method in Python. For a language-agnostic introduction, see Method.

Defining a Method#

Every instance method in Python takes self as its first parameter. self is a reference to the object the method is being called on.

class Player:
    def greet(self):
        print("Hello!")

By convention, method names use snake_case: greet, play, get_score.

Return Values#

Use return to send a value back from a method. A method without a return statement returns None.