A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://pythonspot.com/objects-and-classes/ below:

python class - Python Tutorial

Introduction to Python Classes
The landscape of technology has always been evolving, and so has the way we program. In our journey of understanding Python, we encounter the concept of classes. But before we delve into that, let’s trace back a bit. Where did classes originate from?

1. Statements:
In the initial stages of computing, programmers only wrote commands or statements to communicate with machines.

2. Functions:
To make code more modular and readable, functions were introduced. These are reusable groups of statements that structure the code better.

3. Classes:
Enter the world of classes, the heart of object-oriented programming (OOP). With classes, we create objects. These objects have both functions (methods) and variables. A good example would be strings in Python. For instance, a string variable named ‘book’ has methods like book.replace() and book.lower(). This programming paradigm, where we model real-world objects, is commonly referred to as object-oriented programming.

Let’s further explore this concept.

Recommended Python Course:
Python Programming Bootcamp: Go from zero to hero

Understanding Python Classes
In Python, classes allow us to craft virtual objects. These objects can house both variables (often called attributes) and functions (referred to as methods). For instance, consider the following class:

1
2
3
4
5
6
7
8
class User:
name = ""

def __init__(self, name):
self.name = name

def sayHello(self):
print("Hello, my name is " + self.name)

Here, we have a class User with an attribute name and a method sayHello. We can create instances of this class like so:

1
2
3
james = User("James")
david = User("David")
eric = User("Eric")

James, David, and Eric are all instances (or objects) derived from the User class.

In the User class, we’ve also defined a method sayHello(). That’s why we’re able to call it on each of the objects. The special method __init__() is termed as the constructor. This is automatically invoked whenever a new object is created. Attributes associated with a class, like name in this case, are commonly known as class attributes.

Classes also grant us the capability to define methods that modify the internal state (or attributes) of an object. To illustrate this, let’s dive into another example.

Class Attributes and Their Manipulation
Imagine a scenario where we want to model a coffee machine. This machine will have attributes to store the amount of beans and water. We’d also want methods to add or remove beans and water. Here’s how we can model this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class CoffeeMachine:
name = ""
beans = 0
water = 0

def __init__(self, name, beans, water):
self.name = name
self.beans = beans
self.water = water

def addBean(self):
self.beans += 1

def removeBean(self):
self.beans -= 1

def addWater(self):
self.water += 1

def removeWater(self):
self.water -= 1

def printState(self):
print("Name = " + self.name)
print("Beans = " + str(self.beans))
print("Water = " + str(self.water))

The above class, CoffeeMachine, has attributes name, beans, and water. The methods allow us to manipulate these attributes. For instance:

1
2
3
4
5
pythonBean = CoffeeMachine("Python Bean", 83, 20)
pythonBean.printState()
print("")
pythonBean.addBean()
pythonBean.printState()

On running this, we’d get an output like:

1
2
3
4
5
6
7
Name  = Python Bean
Beans = 83
Water = 20

Name = Python Bean
Beans = 84
Water = 20

This showcases how the internal state of our pythonBean object changes as we invoke methods on it.

Download Exercises to Enhance Your Python Skills

Previous | Next


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4