Source: Kimberly Farmer on Unsplash
Expansion: A class serves as a conceptual blueprint or a template for creating objects. Think of it like the architectural drawings for a house. The blueprint itself isn't a house you can live in, but it provides all the specifications—number of rooms, where the kitchen goes, plumbing, electrical—to build many identical or similar houses.
In programming, a class defines the common characteristics (data/attributes) and behaviors (functions/methods) that all objects created from that class will possess. This logical grouping is fundamental to OOP because it promotes:
Expansion: These are the core components that bring a class to life:
Attributes (Data): Attributes represent the state or characteristics of an object. They are like the nouns that describe an object. For an Employee
class, first
, last
, pay
, and email
are attributes. Each instance of an Employee
will have its own specific values for these attributes (e.g., employee_1
might have first='John'
, last='Doe'
, while employee_2
has first='Jane'
, last='Smith'
). Attributes can be of various data types (strings, numbers, booleans, lists, even other objects).
Methods (Functions): Methods represent the actions or behaviors that an object can perform. They are like the verbs that describe what an object does. The full_name
method for an Employee
class is an action that returns the employee's full name. Methods often operate on the object's attributes to achieve their purpose. They encapsulate the logic related to an object's behavior, keeping the code organized and relevant to the object itself.
pass
KeywordExample Revisited:
class Employee:
pass