Simple Classes


Defining Classes

class MyClassName:
    #
    # By convention, class names use UpperCamelCase (aka PascalCase
    # ...
  • simlpest example:
class SimplestFlight:
    pass

# some-module/main.py
from airtravel import SimplestFlight

f = Flight()
type(f) #=> <class 'airtravel.Flight'>

A Second Class

Airtravel example (Aircraft class)

class Aircraft:

    def __init__(self, registration, model, num_rows, num_seats_per_row):
        self._registration = registration
        self._model = model
        self._num_rows = num_rows
        self._num_seats_per_row = num_seats_per_row

    def registration(self):
        return self._registration

    def model(self):
        return self._model

    def seating_plan(self):
        return (range(1, self._num_rows + 1),
                "ABCDEFGHJK"[:self._num_seats_per_row])
from airtravel import Flight, Aircraft

a = Aircraft("G-EUPT", "Airbus A319", num_rows=22, num_seats_per_row=6)
a.registration() #=> 'G-EUPT'
a.model() #=> 'Airbus A319'
a.seating_plan() #=> (range(1, 23), 'ABCDEF')
Made with Gatsby G Logo