DEV Community

Ronny Mwenda
Ronny Mwenda

Posted on

Classes in Python, a beginner's pov

WHAT IS A CLASS

A class is like a blueprint for creating objects or an object constructor. It defines what data (variables) and actions (functions/methods) an object should have.Classes are a good example of object oriented programming using python.

Classes are used for grouping code, to enable code reusability and scalability, and to facilitate the writing of clean code.

When creating classes we use a special function called a the constructor method, this function gets called automatically and creates a new object.Basically innitializing the objects attributes.

constructors are used to automatically set up object data, avoid repetitive code and ensure that every object starts with valid values.

In Python, a class is created using the CLASS keyword.When creating a class its advised to use the following naming conventions:snake_case for the variable and function, pascal case for the class name and upper snake case for the constant.Example:

  • Variable : my_nephew
  • function : my_nephew()
  • class : MyNephew
  • constants : MY_NEPHEW

syntax of a class with a constructor.

class MyNephew:
def init(self, name, age):
self.name = name
self.age = age

def info_details(self):
    print(f"Hello, my name is {self.name} and I am {self.age} years old.")
Enter fullscreen mode Exit fullscreen mode

my_nephew = MyNephew("John", 10)
my_nephew.info_details()

---the code above creates a class called MyNephew,with a constructor(init), parameters passed into the constructor(name, age),objects(my_nephew & my_cousin) and values assigned to the object(self.name, self.age) and an instance being created(self).Calling the function info.details() the objects use their passed data to print personalised message through an f-string.

Another fitting example of an instance where classes can be used is when creating a mock bank account.

Task:

Create a class called BankAccount.

class BankAccount:
def init(self, account_holder):

    self.account_holder = account_holder
    self.balance = 0
Enter fullscreen mode Exit fullscreen mode

Attributes: account_holder, balance (default = 0).

Methods:

deposit(amount)

def deposit(self, amount):
    self.balance = self.balance + amount
    print(f"you have deposited {amount}.")
    print(f"Your new balance is {self.balance}.")
Enter fullscreen mode Exit fullscreen mode

withdraw(amount)

def withdraw(self, amount):
    if amount <= self.balance:
        self.balance = self.balance - amount
        print(f"You have withdrawn {amount}.")
        print(f"Your new balance is {self.balance}.")
    else:
        print("Insufficient funds for this withdrawal.")


#  show_balance()

def show_balance(self):
    print(f"{self.account_holder} Your current balance is {self.balance}.")

#Creating an account and testing all three methods.
Enter fullscreen mode Exit fullscreen mode

myaccount = BankAccount("JOHN DOE

  • ") myaccount.deposit(1000) myaccount.withdraw(500) myaccount.show_balance().

my_nephew = MyNephew("John", 10)
my_nephew.info_details()

my_cousin = MyNephew("Alice", 12)

my_cousin.info_details()

Top comments (0)