ScanSkill

class

Class is a blueprint for creating individual objects that contain the general characteristics of an object.

Syntax

class ClassName:
    # fields, constructor
    public_member # This can be accessed from outside the class

    # This cannot be accessed from outside the class, shown with two underscores
    __private_member

Notes:

  • The class body contains:
    • constructors for initializing new objects,
    • declarations for the fields that provide the state of the class and its objects,
    • methods to implement the behavior of the class and its objects.

Example

>>> class Bike(object):
...     def __init__(self, gear, speed):
...         self.gear = gear
...         self.speed = speed
... 
...     def apply_brake(decrement):
...         self.speed -= decrement
... 
...     def speed_up(increment):
...         self.speed += increment
... 

ss