Monday, October 9, 2023

Python Classes and Objects (Theory)

Python is an object oriented programming language.

Almost everything in Python is an object, with its properties and methods.

A class is a blueprint of creating an object (like a Constructor).

The keyword to remember is “class” itself.

# For Ex: Creating a class with Name MyClass

class MyClass:

x = 5

# Print created class

print(MyClass)

Output : <class '__main__.MyClass'>

Creating an Object

# Create an object named p1, and print the value of x

class MyClass:

x = 5

p1 = MyClass() # Object creation using constructor

# Accessing the property of an Object using the dot (.) notation.

print(p1.x)

Output : 5

Define an Empty class

A class body can’t be empty , if we have nothing to define then need to use pass statement to ignore error message

# Defining an empty class

class Person :

pass

The __init__() Function and Classes

  • All classes have a function called __init__()
  • It is always executed when an object is being created
  • We can use it to assign values to object properties &
  • To perform other necessary operations required at the time of object creation

# Create a class named Person, and use the __init__() function to assign values for name and age:

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

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
p1 = Person("John", 36)
print(p1.name)
print(p1.age) 

A word about ‘self’

Simply stated, self refers to an instance itself.

For now, just know that when we want to refer to instance variables in the class, we need to add self in front of the variable names.

In addition, most methods in a class have self as the first parameter.

Object Methods

  • Class can contain methods for objects

# For Ex – Defining a method that print a message on its call by object

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

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

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

Output : Hello my name is John

Note : The self parameter is a reference to the object of the class, and is used to access properties of the object

Note : First parameter of a method always refer to the current object , no matter what its name.

Function and Method

Function is usually understood as a standalone block of code.

>>> m = lambda a, b: a * b

>>> m(5, 5)

25

>>> m

<function <lambda> at 0x7f69cd84d280>

>>>

Method is understood as a funtion associated with a class or it’s object.

class Calc:
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def add(self):
        return self.a + self.b

    def mul(self):
        return self.a * self.b
    
    def sub(self):
        return self.a - self.b
sub() has no existence outside of Calc.

Modify and Deleting Object Properties

# For Ex – Modify the age of Object 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 # Modify the age for object p1

del p1.name # Deleting the name property for object p1 by using del keyword

print(p1.age) # Output will be : 40

print(p1.name) # Output will be : AttributeError: 'Person' object has no attribute ‘name'

Deleting an Object

  • An object can be deleted by using del key word as well
  • # For Ex - 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 :

    NameError: 'p1' is not defined

No comments:

Post a Comment