Python Classes

  • Post author:
  • Post comments:0 Comments
  • Reading time:33 mins read

Introduction to Python Classes

In Python, a class is a template for creating objects. Objects have member variables and have behavior associated with them. In python, a class is created by the keyword class.

An object is created using the constructor of the class. This object will then be called the instance of the class.

Defining a Class

To define a class, you use the class keyword followed by the class name and a colon. The class body consists of all the class member variables and functions.

Here is an example of a simple Python class:


class MyClass:
  x = 5

Creating an Object

To create an object of a class, you can use the class name followed by parentheses.


obj = MyClass()

Now the object obj has been created and you can access the member variables of the class using the dot notation:


print(obj.x)  # Output: 5

The __init__ Function

The __init__ function is a special function in Python classes that is used to initialize the member variables of the class. It is called automatically every time an object of the class is created.

Here is an example of how to use the __init__ function:


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


p1 = Person("John", 36)

print(p1.name)  # Output: John

print(p1.age)   # Output: 36

In the example above, we defined a Person class with the __init__ a function that takes two arguments: name and age. These values are then assigned to the member variables self.name and self.age, respectively.

When we create an object of the Person class and pass in the values “John” and 36, the object p1 is created with the member variables name and age initialized to “John” and 36, respectively.

Class Members

In Python, member variables of a class are variables that are defined inside the class body and are accessible to all the member functions of the class.

Here is an example of how to define and access class member variables:


class MyClass:
  x = 5
  y = 6


obj = MyClass()

print(obj.x, obj.y)  # Output: 5 6

Class Methods

In Python, a method is a function that is defined inside a class and is used to perform operations on the member variables of the class.

To define a method in a class, you use the def keyword followed by the method name and the method arguments, just like you would with a regular function.

Here is an example of how to define and call a method in a Python class:


class MyClass:
  x = 5

  def printX(self):
    print(self.x)


obj = MyClass()

obj.printX()  # Output: 5

In the example above, we defined a MyClass class with a single-member variable x and a method printX that prints the value of x.

The self parameter is a special parameter in Python classes that is used to refer to the current instance of the class. It is automatically passed to the method when the method is called on an object.

Publisher

Publisher

Publisher @ideasorblogs

Leave a Reply