Concepts Of Python

Programming techniques known as object-oriented programming (OOP) aggregate related fields, or data members, and procedures into objects. It offers a method for organising programs so that the data stored and the activities carried out on it are grouped into classes and made available through objects. With this paradigm, programmers can better organise and create their programs by modelling real-world entities as objects.
Class objects and instance objects are the two types of objects in Python object-oriented programming architecture. Classes, Objects, and Instances are the fundamental ideas to comprehend.
Class
Lesson A class is an object creation blueprint, template, or factory. It specifies the capabilities and structure of the objects made from it. What an item contains is indicated by its class. It is a form of data structure definition that blends code (functions, also known as methods) and data (variables, also known as attributes or fields). The characteristics and actions of objects are defined by a class.
To build a new object type that hasn’t been made before, you define a class. The class keyword appears first in the definition of a class, and then the class name. Code that is indented beneath the class definition is regarded as belonging to the body of the class. By convention, class names begin with a capital letter. Classes are attributes of a module object and are always nested inside a module.
Object / Instance
The item or instance A distinct instance of a class is called an object. In contrast to a class, which is merely a conceptual entity or blueprint, it is the actual implementation of a class and exists in reality. The tangible things that your programs process are called objects. A class provides the specification that is used to construct an object. An instance is an object created from a class that contains actual data, whereas the class is the blueprint.
A common class is used to define objects of the same kind. A single class can contain several objects (commonly called instances) at once, each of which may have unique properties. Every occurrence is different. An instance of a class is what you get when you build an object for a class. A class’s instance and object are frequently used interchangeably. Instantiation is the process of generating an object from a class.
Attributes
Qualities An object’s attributes are its data. They are sometimes referred to as instance variables or fields. Information about the item is contained in its attributes. An object’s “state” at any one time is determined by the values that its attributes currently hold. Attributes may be shared by all instances (commonly referred to as class attributes) or unique for each instance (typically referred to as instance variables or data attributes).
A class’s instance variables are kept for a single instance. For example, variables have their own values. Instance variables that are specific to every object in a class are called data attributes. All objects belonging to a class share class variables, or class attributes.
Methods
Techniques Functions defined inside a class are called methods. They specify the actions or functions that an object is capable of carrying out or reacting to. Methods are class functions. An object’s interactions with other objects are defined by its methods. A class of objects’ shared behaviour is represented by instance methods. In Python, instance methods get self, the instance, as their first parameter. The class, usually cls, is the first parameter of class methods, which affect the class as a whole and are denoted by @classmethod.
Code Example
This straightforward example shows a class, creates instances (objects), and accesses their characteristics and methods.
# Define a Class called Dog
class Dog: # Class names usually start with a capital letter
# Class attribute: shared by all instances of this class kind = 'canine'
# The init method is a special method, known as an initializer or constructor
# It is automatically called when a new object (instance) is created
# The first parameter 'self' refers to the instance being created
def init (self, name, age):
# Instance attributes: unique to each instance
self.name = name # Create and set the 'name' attribute for the instance
self.age = age # Create and set the 'age' attribute for the instance
print(f"A new dog named {self.name} has been created.")
# Instance method: a function associated with the object
# 'self' refers to the specific instance the method is called on
def description(self):
return f"{self.name} is {self.age} years old."
# Another instance method
def speak(self, sound):
return f"{self.name} says {sound}"
# Create objects (instances) from the Dog class
# This is called instantiation
# Calling the class like a function creates a new instance object
dog1 = Dog('Willie', 6) # Passes 'Willie' and 6 to the init method
dog2 = Dog('Buddy', 4) # Creates another instance
# Access attributes and call methods using the dot operator (.)
print(f"My dog's name is {dog1.name}.")
print(f"My dog is {dog1.age} years old.")
print(dog1.description())
print(dog1.speak("Woof Woof"))
print(f"Another dog's name is {dog2.name}.")
print(f"Another dog is {dog2.age} years old.")
print(dog2.speak("Bow Wow"))
# Access the class attribute
print(f"Dog1 is a {dog1.kind}")
print(f"Dog2 is a {dog2.kind}")
print(f"All dogs are {Dog.kind}")
In this example:
- The class is Dog. It is the blueprint that specifies the characteristics of a dog object, such as its kind, name, age, description, and speech capabilities.
- Dogs 1 and 2 are instances or objects of the Dog class. These are tangible insights derived from the Dog blueprint.
- Kind is an attribute of a class. All Dog objects have the same value (‘canine’).
- Age and name are examples of qualities. The names and ages of each dog item are distinct (‘Willie’ and 6 for dog1, ‘Buddy’ and 4 for dog2).
- The methods are speak, description, and init. While talk and describe are instance methods that provide a Dog object’s behaviours, init is a special constructor method.