Python

 

 

 

 

Python - Class

 

Calling a Python class refers to the process of creating an instance of the class and interacting with it. Classes in Python are templates that define the structure and behavior of objects. They encapsulate data (attributes) and methods (functions) that work on the data. When you call a class, you are essentially creating an object of that class and working with its methods and attributes.

 

Here's a high-level overview of calling a Python class:

  • Define the class: First, you need to define the class by using the class keyword followed by the class name. The class definition includes a set of methods and attributes that dictate the behavior and state of the objects created from it.
  • Initialize the class: To set up the initial state of a class instance, define an __init__ method within the class. This method, also known as the constructor, is called automatically when you create a new instance of the class. The __init__ method typically accepts arguments to initialize the object's attributes.
  • Create a class instance: To create an instance of the class, you call the class name followed by parentheses, passing any required arguments for the __init__ method. The syntax is class_instance = ClassName(arguments). This creates a new object of the class and initializes it using the provided arguments.
  • Access attributes: After creating a class instance, you can access its attributes using the dot (.) notation. The syntax to access an attribute is class_instance.attribute_name. If the attribute is public (not prefixed with an underscore), you can access it directly. If the attribute is private (prefixed with one or two underscores), it is generally accessed through getter methods.
  • Call methods: Methods are functions defined within the class that perform actions on the object or its data. To call a method, you use the dot (.) notation followed by the method name and parentheses, passing any required arguments. The syntax is class_instance.method_name(arguments).
  • Manage object lifetime: Python automatically manages the lifetime of objects through reference counting and garbage collection. When an object is no longer needed, Python will automatically clean up its memory. However, if the object holds resources like file handles or network connections, you might need to define a destructor method __del__ to release those resources when the object is no longer in use.

 

 

 

Examples

 

NOTE 1 : All the examples in this page are written in Python 3.x. It may not work if you use Pyton 2.x

NOTE 2 : All the examples in this page are assumed to be written/run on Windows 7 unless specifically mentioned. You MAY (or may not) need to modify the syntax a little bit if you are running on other operating system.

  • Defining a Class  - Example 1
  • Defining a Class with Constructor   - Example 2
  • Defining a Class with Constructor Overloading  - Example 3
  • Defining a Class Inherited from other class - Example 4

 

 

< Example 1 > Defining Class

 

class myInteger:

    

    value = 0  # define a global variable within the class.

               # you can use this variable in anyware within the class by

               # self.value

    

    def increment(self, step = 1):

        self.value +=  step

        

    def decrement(self, step = 1):

        self.value -= step

        

    def getValue(self):

        return self.value

 

 

myInt = myInteger()  # create an instance of the class

 

print(myInt.getValue())

 

myInt.increment()

print(myInt.getValue())

 

myInt.increment(4)

print(myInt.getValue())

 

myInt.decrement(2)

print(myInt.getValue())

 

myInt.decrement()

print(myInt.getValue())

 

 

Result :------------------------------

 

0   <--- myInt = myInteger()

1   <--- myInt.increment()

5   <--- myInt.increment(4)

3   <--- myInt.decrement(2)

2   <--- myInt.decrement()

 

 

 

< Example 02 >  Defining a Class with Constructor

 

class myInteger:

    

    value = 0

 

    def __init__(self):

        self.value = 10

        print("Initial Value = ",self.value)

    

    def increment(self, step = 1):

        self.value +=  step

        

    def decrement(self, step = 1):

        self.value -= step

        

    def getValue(self):

        return self.value

 

 

myInt = myInteger()

 

 

Result :------------------------------

 

Initial Value =  10   <--- myInt = myInteger()

 

 

 

< Example 03 >  Defining a Class with Constructor Overloading

 

class myInteger:

    

    value = 0

 

    def __init__(self, initialValue = 1):

        self.value = initialValue

        print("Initial Value = ",self.value)

    

    def increment(self, step = 1):

        self.value +=  step

        

    def decrement(self, step = 1):

        self.value -= step

        

    def getValue(self):

        return self.value

 

 

myInt1 = myInteger()

myInt2 = myInteger(10)

myInt3 = myInteger(initialValue = 5)

 

 

Result :------------------------------

 

Initial Value =  1    <--- myInt1 = myInteger()

Initial Value =  10   <--- myInt2 = myInteger(10)

Initial Value =  5    <--- myInt3 = myInteger(initialValue = 5)

 

 

 

< Example 04 > Inherit another class

 

class Animal:

 

    species = ""

    def __init__(self,species):

        self.species = species

 

    def getSpecies(self):

        return self.species

 

 

class Dog(Animal):  # Define a class Dog which inherits the class Animal

     def __init__(self):

        super().__init__("Dog")

 

dog = Dog(); 

print(dog.getSpecies())

 

 

Result :------------------------------

 

Dog

 

 

< Example xx >