Search⌘ K
AI Features

Classes and Inheritance

Explore how to create and manage Python classes, understand inheritance to share variables and methods, and use object-oriented concepts like instances and self to build flexible programs.

Class definition

A class describes a new type of object and bundles together the methods needed to work with objects of that type. Here is an analogy: a class is like a cake recipe, while objects are the cakes you can make by following the recipe.

Defining a class

The syntax for defining a class is

class ClassName(superclass):
 variable and method definitions

Introduction to inheritance

Every class (except the object class) has a superclasssuperclass, and it inherits variables and methods from that superclass. That is, all variables and methods defined in (or inherited by) the superclass are available to the new class.

For example, you might define a class Person with the variables name and age and a method greet. Every object of this type will have its own copies of those variables, and its own reference to the greet method. If you then create a class Customer as a subclass of Person, every object of type Customer will have its own name and age variables and a greet method. Plus, it will have whatever additional variables and methods you declare in Customer (for example, a list of purchases).

code_drawing

Introduction to the object class

The special object class is the “root” of all classes. If you omit superclass when you define a class, it defaults to having the superclass object. Every class inherits from object, either directly or indirectly.

Terminology: As a noun, “instance” means the same as “object.” However, we usually use “instance” when talking about a particular object (john is an instance of Person), or when we are using the word as an adjective (name is an instance variable of the class Person).

Terminology: “Field” is another name for “instance variable."

Here’s an example of a class definition:

class Person(object):
  # defining variables of Person class
  name = 'Joe'
  age = 23

  # defining methods
  def say_hi(self):
    print("Hello, Joe.")

There are numerous things that we can do with the above example:

  • To create an instance (object) of a class, we can use the name of the class as if it were a function name, for example, p = Person().
  • We can access the fields of object p using dot notation: p.name is “Joe” and p.age is 23.
  • We can modify the fields of p, for example, by saying p.name = 'Jack'.
  • We can also use the dot notation to “talk to” the object p or, more formally, “send a message to” the object p: p.say_hi() tells p to say_hi, and p will respond by printing Hello, Joe..

Convention: Class names always begin with a capital letter, and variable and function names always begin with a lowercase letter.

The following code-based example has been provided to help you better understand these concepts.

Python 3.5
class Person(object):
# defining variables of Person class
name = 'Joe'
age = 23
# defining methods
def say_hi(self):
print("Hello, Joe.")
p = Person() # creating instance of Person
print('name:', p.name, 'age:', p.age) # accessing the name and age variables of Person
p.name = 'Jack' # modified the name of Person class
print('name:', p.name, 'age:', p.age) # can be seen here (name -> different, age -> same)
p.say_hi() # calling say_hi function

Unfortunately, every instance we create from this class will be exactly the same. We would like to create instances of Person with different values for name and age. To do this, we have to explore a special variable: self.