GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • About
  • Reference
  • Integrations
  • API
  • Blog
  • Python Syntax Reference
  • Feedback

This view

  • Original page
  • New address

GeezerVison is showing syntaxdb.com — Class Declaration & Structure in Python - SyntaxDB - Python Syntax Reference

  • Java
  • C
  • C++
  • C#
  • Python
  • Ruby
  • JavaScript
  • Swift
  • Go
Languages
    • User Program Communication
      • Print
      • User Input
      • Commenting
      • Read from Text File
      • Write to File
      • Import Module
    • Variables
      • Variable Declaration
      • Lists
      • List Comprehensions
      • Tuples
      • Dictionaries
      • Sets
    • Control Flow
      • If Statement
      • For Loop
      • While Loop
      • Ternary Operator
      • Try Catch Block
      • With Statement
      • Generators
    • Functions
      • Functions
      • First Class Functions
      • Lambda
      • Map
      • Filter
    • Object Oriented Programming
      • Class Declaration & Structure
      • Class and Instance Variables
      • Constructor
      • Instantiation
      • Methods
      • Inheritance
Categories
Language
Python
  • Java
  • C
  • C++
  • C#
  • Python
  • Ruby
  • JavaScript
  • Swift
  • Go
Version: 3.4.3

Categories

  • User Program Communication
    • Print
    • User Input
    • Commenting
    • Read from Text File
    • Write to File
    • Import Module
  • Variables
    • Variable Declaration
    • Lists
    • List Comprehensions
    • Tuples
    • Dictionaries
    • Sets
  • Control Flow
    • If Statement
    • For Loop
    • While Loop
    • Ternary Operator
    • Try Catch Block
    • With Statement
    • Generators
  • Functions
    • Functions
    • First Class Functions
    • Lambda
    • Map
    • Filter
  • Object Oriented Programming
    • Class Declaration & Structure
    • Class and Instance Variables
    • Constructor
    • Instantiation
    • Methods
    • Inheritance

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 >


You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.