Welcome to
On Feet Nation
Python classes and objects
In this tutorial, you will learn about the core functionality of Python classes and objects. You will learn what a class is, how to create it and use it in your program.
Python Objects and Classes
Python is an object-oriented programming language. Unlike procedural programming, where the main emphasis is on functions, object-oriented programming emphasizes objects.
An object is simply a collection of data (variables) and methods (functions) that act on that data. Similarly, a class is a model for that object.
We can think of the class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows, etc. Based on these descriptions we build the house. The house is the object.
Since many houses can be made from a house plan, we can create many objects from one class. An object is also called an instance of a class, and the process of creating this object is called instantiation.
Defining a class in Python
Just as function definitions begin with the def keyword in Python, class definitions begin with a class keyword.
The first string inside the class is called docstring and has a short description of the class. Although it is not required, it is highly recommended.
Here is a Python class definition.
MyNewPlace class:
'' 'This is a doc string. I have created a new class' ''
pass
A class creates a new local namespace where all its attributes are defined. Attributes can be data or functions.
© 2024 Created by PH the vintage. Powered by
You need to be a member of On Feet Nation to add comments!
Join On Feet Nation