Understanding the concept of class and object into the Python.

Class: A class is a blueprint of object. It can also use to make the templets for objects.

  • Class can provide the initial values for state and implementation of behaviour.
  • The user-define objects are created using the “class” keyword.

Example:

class Company:          # Use the class keyword for create the class in python
       company_name:"Vanadan Industries"       # Body of the class
       company_emps: 20000

 

Object: Object is the instance of the class. This will used to define the class.

  • It can used to access the properties of the class.
  • Object can create after the creating the class.
  • It is the concept of Object-Oriented Programming (OOP).

Example: Create the object using the above class code.

obj = Company()      # Object are classified by the obj function
print(obj.company_name)    # Print the program using this object.
print(obj.company_emps)

 

Now to understand the class and object by the using the full new python code with the output:
class Animal:
    def __init__(self, name, species, breed):
        self.name = name
        self.species = species
        self.breed = breed

obj_animal = Animal("Buddy","Dog", "Golden Retriever")
print("Animal Name: ",obj_animal.name)
print("Animal Species: ",obj_animal.species)
print("Animal Breed: ",obj_animal.breed)
Output:

Animal Name: Buddy
Animal Species: Dog
Animal Breed: Golden Retriever

 

Another code of class and object using the Inheritance in python:
class Animal:
    def __init__(self, name, species):
        self.name = name
        self.species = species

    def speak(self):
        print(f"{self.name} makes a sound.")

class Dog(Animal):
    def __init__(self, name, breed):
        Animal.__init__(self, name, species= "Dog")
        self.breed = breed

    def speak(self):
        print(f"{self.name} barks.")
        Animal.speak()

obj1 = Dog("Buddy", "Golden Retriever")
print("Animal Name: ", obj1.name)
print("Animal Species: ", obj1.species)
print("Animal Breed: ", obj1.breed)
Output:

Animal Name: Buddy
Animal Species: Dog
Animal Breed: Golden Retriever

Below link can help to view the code that can created by me on the vs code editor.

https://github.com/virpatidar/Python-1

Class and Object can used in every OOP concept of the python, and it will help to understand the simple concepts of the programming. It is very easy to use in python rather than other languages

https://coderspacket.com

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top