Python classes and objects are made easy with examples

()

In our daily lives, everything can be considered an object, each having its own set of characteristics (attributes) and behaviors (actions).

Daily Life Examples:

  • Object: Mobile Phone
    • Attributes or Characteristics: Brand, Screen Size, Operating System
    • Behaviors: Making Calls, Sending Text Messages, Running Apps
  • Object: Computer Monitor
    • Characteristics: Screen Size, Resolution, Refresh Rate
    • Behaviors: Displaying Images, Videos, Text
  • Object: CPU (Central Processing Unit)
    • Characteristics: Clock Speed, Core Count, Cache Size
    • Behaviors: Running Software, Performing Calculations, Managing Data.

Learn more with the below words:

  • Object Attributes: Characteristics, features, types, color, weight, width, height, extensions, size.
  • Object Behaviours: Functions, Actions, Decision making, Communication, Reactions, Relationships, and Conditions are all related to object behaviors.

Why we use class to define object attributes and behaviors?

Class: Class defines the nature or type of the object which includes (object attributes + behaviors). Using a class in programming is a systematic and organized way to design, organize, arrange, manipulate, and process complex or large amounts of data, and inputs, faster and more effectively.

The benefit of using classes is that you do not have to create separate functions or logic to organize new objects. You can categorize that into the related class. For example, when you bring a new computer or mobile or CPU you can easily assign or use the already defined attributes and behaviors.

That’s why we say that a class is a blueprint or template for defining the attributes and behaviors of an object.

Suppose there are 100,000+ cars (cars – object) in the market and each car may or may not have unique features and functions. Now, how do you categorize them?

There are 788.84+ crores of humans (human – -objects) on this earth, how do you categorize them?

This is a problem. It is because you want to create a specific process or method or policy or marketing strategy or to create software related to them. If you want to provide personalized user experience then you can categorize them and impact on all you need to organize them effectively. In simple, you want to do this task faster with less effort so that you do not have to repeat the code.

To solve this, obviously, you can categorize cars based on their manicuring, brand, model, color, electric, hybrid, oil engine, and engine. This is an object. Now you can also categorize these based on their behavior (actions) such as speed, driving comfort, gearing, steering, braking, etc.

Categorizing Cars:

  • By Characteristics: Manufacturer, Model, Color, Engine Type.
  • By Behaviours: Speed, Comfort, Handling.

Categorizing Humans:

  • By Characteristics: Citizenship, Country, Sex, Height.
  • By Behaviours: Politicians, Leaders, Doctors,

Most simplest examples of classes, objects, attributes, and actions are data folders on our computers and mobile. 

You see that music is categorized with a music folder. You added a new song in the music folder because that is the song (object) and the behavior is that you can listen to that song.

That folder is a class that you have used to organize songs, rename a list of songs, copy, delete, move, etc.

In one line, you can say that folders are like classes that help organize files based on their attributes and actions (like playing music), rename files, and delete files.

In our daily lives, we encounter countless objects, each with its own set of characteristics and behaviors. In the world of software, mobile apps, and web applications, we use the power of computer programming languages to create software to do such complex activities or to create new innovative solutions.

Now, let’s transition from the concept of classes and objects to practical coding by creating a simple Python program with objects and classes.

The below program defines a class YouTuber, creates two instances of that class, assigns values to their attributes, and then prints out the total subscribers for youtuber1 and `youtuber2. The code is used for educational purposes or as a demonstration of Python’s object-oriented programming (OOP) concepts, specifically the creation of classes and objects.

# Define the YouTuber class with attributes.
class YouTuber:
    name = ""
    channel_name = ""
    subscribers = 0

# Create two YouTuber objects (instances).
youtuber1 = YouTuber()
youtuber2 = YouTuber()

# Assign attribute values for youtuber1.
youtuber1.name = "Vijay Sharma"
youtuber1.channel_name = "Klient Solutech"
youtuber1.subscribers = 10000

# Assign attribute values for youtuber2.
youtuber2.name = "Emily"
youtuber2.channel_name = "Emily Marketing Episode"
youtuber2.subscribers = 20000

# Access object attribute values and calculate the total subscribers.
total_subscribers = youtuber1.subscribers + youtuber2.subscribers

# Print the results.
print(f"YouTubers: {youtuber1.name} and {youtuber2.name} total subscribers are: {total_subscribers}")

Now let’s understand this code in more detail:


We created the class using the class keyword (syntax) and named it YouTuber.

class YouTuber:

name = ""

channel_name = ""

subscribers = 0
  • Object Attributes: Characteristics – that are used and accessed later.
  •  But need to define it in a class so that we can organize the code more effectively.
  •  You can think of it like we are creating a template or folder to organize YouTubers.
  •  You can think of these as a field connected with a class or box contained in a container.
  •  We created these attributes for objects. So that our object fits into these.

youtuber1 = YouTuber()
youtuber2 = YouTuber()
  • It demonstrates how to create instances (objects) of the YouTuber class (youtuber1 and youtuber2).
  • Objects are instances of classes, and they inherit the attributes and behaviors defined in the class.
  • Let’s create a class instance.
  • Request the class
  • To create objects, you have to create class instances.
  • youtuber1 and youtuber2 are instances of the YouTuber class.

  • Now let’s access the class attribute. We can access the field inside the class that is name, channel_name. Or access to a template or blueprint.
  • Access attribute or use or transfer and assign values or add values.
  • And how each instance can have its own attribute values.
  • It illustrates how to set attribute values for objects, such as the name, channel name, and number of subscribers.
youtuber1.name = "Vijay Sharma"
youtuber1.channel_name = "Klient Solutech"
youtuber1.subscribers= 10000
youtuber2.name = "Emily"
youtuber2.channel_name = "Emily Marketing Episode"
youtuber2.subscribers = 20000

Now let’s access the attribute values or do anything further or create further code such as methods, functions or class methods to work with values. But for example, let’s use print inbuilt python function.

  • The code calculates and prints the total number of subscribers by combining the subscriber counts of youtuber1 and youtuber2. This demonstrates how objects can interact and share information.
  • It shows how to access and use the attribute values of objects, including performing operations on those attributes.
# Access object attribute values and calculate the total subscribers.
total_subscribers = youtuber1.subscribers + youtuber2.subscribers
# Print the results.
print(f"YouTubers: {youtuber1.name} and {youtuber2.name} total subscribers are: {total_subscribers}")

Output

YouTubers: Vijay Sharma and Emily total subscribers are: 30000

Thank you for reading about classes and objects in this post! We hope you found it informative and enjoyable.

To stay updated with our latest articles and continue exploring exciting programming and software development concepts and topics, be sure to bookmark our website and follow us on social media. We look forward to sharing more valuable content with you in the future. Stay tuned!

See video tutorials about classes and objects in Hindi

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Found this helpful? Let others know!
0Shares

Leave a Comment

X
Latest Posts
not found 404 error google search console

Google Search Console 404 Errors: An SEO Warning