Home Coding & Scripting Classes and Objects in Python

Classes and Objects in Python

270
0

In Python, everything is built around objects. Classes and objects let you combine data and behavior in one structure, making your programs easier to organize and reuse.


📝 If You Want to Practice the Code

If you plan to practice the Python code mentioned within this blog post, you’ll need a working Python environment. You can execute the commands in one of the following ways (choose only one option):

👉 Choose whichever environment you’re most comfortable with. If you haven’t set up Python yet, start with one of the installation posts before trying the examples here.


Understanding Classes and Objects

  • A class is like a blueprint.
  • An object is a specific item created from that blueprint.

🔹 Creating a Class

A class is defined using the class keyword.

class Student:
    pass

Here, Student is an empty class.


🔹 Making an Object

Objects are created by calling the class:

s1 = Student()
print(type(s1))

👉 Output: <class '__main__.Student'>


🔹 Adding Attributes

Classes can store data using attributes. Attributes are usually set in a special method called __init__.

class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age

s1 = Student("Alice", 20)
s2 = Student("Bob", 22)

print(s1.name, s1.age)   # Alice 20
print(s2.name, s2.age)   # Bob 22
  • __init__ is the constructor: it runs when the object is created.
  • self refers to the current object.

🔹 Adding Methods

A class can also contain methods — functions that belong to the class.

class Student:
    def __init__(self, name, grade):
        self.name = name
        self.grade = grade

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

    def show_grade(self):
        print(self.name, "scored grade", self.grade)

s1 = Student("Alice", "A")
s1.greet()        # Hello, my name is Alice
s1.show_grade()   # Alice scored grade A

🔹 Multiple Objects

Each object keeps its own copy of attributes:

s2 = Student("Bob", "B")
s2.greet()        # Hello, my name is Bob
s2.show_grade()   # Bob scored grade B

📝 Mini Task

  1. Create a class Car with attributes brand and year.
  2. Add a method drive() that prints: "Brand is driving".
  3. Create two car objects and call the method.
class Car:
    def __init__(self, brand, year):
        self.brand = brand
        self.year = year

    def drive(self):
        print(self.brand, "is driving")

c1 = Car("Toyota", 2020)
c2 = Car("Tesla", 2022)

c1.drive()   # Toyota is driving
c2.drive()   # Tesla is driving

✅ Summary

  • Class → A blueprint for creating objects.
  • Object → An instance of a class.
  • __init__ → Constructor that sets up attributes.
  • self → Refers to the object itself.
  • Methods → Functions inside a class that use or modify its data.

With classes and objects, you can build more structured and reusable programs.

Previous articleCallbacks in Python
Next articleProgram Entry Points in Python
Heartin Kanikathottu
As a seasoned Cloud and Security Architect, I’ve led transformative initiatives in key roles, including Vice President at Morgan Stanley, Principal Architect at Societe Generale, and Tech Lead & Cloud Security Architect at VMware, among others. I’m also an internationally published author with multiple books available on platforms like Amazon and O'Reilly. Notably, one of my books was recognized as the 8th best cloud computing book of all time in 2020, reflecting the impact of my contributions to the field. With over 15 professional certifications from providers such as Microsoft (Azure), Amazon (AWS), Oracle (Java), Pivotal (Spring), and IBM, I bring a wealth of expertise to my work. Academically, I hold dual Master’s degrees in Cloud Computing and Data Analytics. I’m passionate about sharing knowledge and mentoring others, which is why I actively speak at global technical forums such as Tech Opportunities Fest at Platform Calgary, Google's Kubernetes Meetup, Java User Group, Elasticsearch Meetup, and the Agile India Conference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here