Python Classes and Objects


Python is an object oriented programming language.

In Python almost everything has its properties and methods as an object.

A Class or a "blueprint" to generating objects is like a constructor.

You can also search for these topics, python classes and objects example, advance the classes and objects, difference between classes and objects in python, classes and objects keyword using python, python program classes and objects, how to create classes and objects in python, modules classes and objects using python, what are classes and objects in python, learn classes and objects in python, create class objects in loop python.

Create a Class

Use the term class to build a class :

Example :- Create a class called MyClass that contains the property x :

class MyClass:
  x = 5

print(MyClass)

Output :-

< class '__main__.MyClass'>

You can also search for these topics, python how to create a class, create a dynamical class in python, define the method to class creation in python, python create class definition, use the exception of class creation with python, how to create a matrix and programing python class, create a process and package for python class, Example for python create a class.

Create Object

Now we can make objects using the MyClass class:

Example :- Create the p1 name object and print the x value :

class MyClass:
  x = 5

p1 = MyClass()
print(p1.x)

Output :-

5

You can also search for these topics, python create object from user input, create the copy of python object, dynamical value to Create Object in python, python example to Create Object, how to use object empty value, python to generate error object.

The __init__() Function

The previous examples are the most simplistic classes and objects and are not very suitable for applications of real life.

We must comprehend the __init__() built-in function for understanding the meaning of classes.

Every class has a function named __init__(), which always is performed when you start the class.

Use the function __init__() to assign object property values or other essential activities to do while creating the object :

Example :- Use the __init__() function to allocate values for name and age to create a class called the person :

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

p1 = Person("John", 36)

print(p1.name)
print(p1.age)

Output :-

John
36

Note : Whenever a class is used to create a new object, __init__() is automatically invoked.

You can also search for these topics, python the init function best practices, definition for python __init__() Function, use the __init__() Function default value in python, python default argument __init__() Function, Example for python function __init__(), python generator __init__() Function, global and get name Function with python __init__().

Object Methods

Objects may have methods as well. Object methods are functions that are part of the object.

Let us in Person Class build a method :

Example :- Insert a feature which prints a welcome and execute it on the object p1 :

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

  def myfunc(self):
    print("Hello my name is " + self.name)

p1 = Person("John", 36)
p1.myfunc()

Output :-

Hello my name is John

Note : The Self is a reference to the current class instance and is used to access class variables.

You can also search for these topics, python object method list, return value of object method in python, object method example using python, object python keyword methods, python key value and key error object methods, check the exists key error with python methods, example for python object methods.

The self Parameter

The self parameter refers to the current class instance and is used to access class-related variables.

It doesn't need to be called self, but it must be the primary parameter for any function in the class :

Example :- Instead of self use the terms mysillyobject or abc :

class Person:
  def __init__(mysillyobject, name, age):
    mysillyobject.name = name
    mysillyobject.age = age

  def myfunc(abc):
    print("Hello my name is " + abc.name)

p1 = Person("John", 36)
p1.myfunc()

Output :-

Hello my name is John

You can also search for these topics, python the self parameter unfilled, self Parameter by value in python, The self Parameter best practices in python, working of python self parameter function, self parameter keyword meaning using python, python define the keyerror with self parameter, self length of parameters in python, Example for python parameter called self.

Modify Object Properties

You can change objects such like this :

Example :-

Set the age of p1 to 40 :

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

  def myfunc(self):
    print("Hello my name is " + self.name)

p1 = Person("John", 36)

p1.age = 40

print(p1.age)

Output :-

40

You can also search for these topics, python modify object properties access, compare the Modify Object Properties in python, how to count Modify Object Properties with python, python modify object property event, evaluate the object to modify property in python, python Modify Object Properties get value, python modify object properties how to print, properties key Object Properties to Modify using python, Example for Modify python object property.

Delete Object Properties

The del keyword allows you to remove property on objects :

Example :- Delete the object p1 from age property :

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

  def myfunc(self):
    print("Hello my name is " + self.name)

p1 = Person("John", 36)

del p1

print(p1)

Output :-

Traceback (most recent call last):
File "demo_class8.py", line 13, in < module>
print(p1)
NameError: 'p1' is not defined

You can also search for these topics, python delete object properties access, compare the delete Object Properties in python, how to count remove Object Properties with python, python destroy object property event, evaluate the object to delete property in python, python delete Object Properties get value, python delete object properties how to print, properties key Object Properties to destroy using python, Example for remeove python object property.

Delete Objects

By employing del keyword, you can remove items.

Example :- Delete the p1 object :

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

  def myfunc(self):
    print("Hello my name is " + self.name)

p1 = Person("John", 36)

del p1

print(p1)

Output :-

Traceback (most recent call last):
File "demo_class8.py", line 13, in < module>
print(p1)
NameError: 'p1' is not defined

You can also search for these topics, how to delete object python, python how to make a objects delete hashable, delete objects keyword from memory using python, python delete keyword object from list, get the python delete objects keyword instance, python function to del keyword, find the key of python delete object, Example for python delete keywords.

The pass Statement

class definitions may not be empty, but if you have a class definition without content for any reason, please use the pass declaration to prevent an error.

Example :- class Person :

class Person:
  pass

# having an empty class definition like this, would raise an error without the pass statement
You can also search for these topics, python the pass statement example, what is the python pass statement purpose, python pass block statement, pass statement continuation with python, how to check the pass statement using python, python the pass statement conditional, python use pass statement to command, Example for python statement of pass.