Part 8 Python Classes

You can get a long way with Python’s built in type, but if they are not fit for the job you want, this is when Class is needed.

Classes define the structure and behavior of objects and object class control its initialization. Class make complex problems tractable, can make simple solutions overly complex.

#airtravel.py
class Flight:
    def __init__(self, number):
        self._number = number

    def number(self):
        return self._number
    pass

from airtravel import Flight

f = Flight("TORO")
f.number()
  • Method – a function defined within a class
  • Instance methods – functions which can be called on objects
  • Self – the first argument to all instance methods
  • __init__() is an initializer, not a constructor.
  • self is similar to this in C++ or Java.
  • avoid name class with number(), we use _number

Leave a Reply

Your email address will not be published. Required fields are marked *