Object Oriented Programming

Class Declaration & Structure in Python

Classes are a blueprint for creating individual objects that contain the general characteristics of a defined object type. A modifier may or may not be used to declare a class.


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

    #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, and methods to implement the behaviour of the class and its objects.


Example
class Bike(object):
    #constructor
    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

See Also
Documentation

Classes - Python Documentation

Add to Slack

< Filter   |   Class and Instance Variables >

© 2019 SyntaxDB. All Rights Reserved.