Objects and Classes in Python Tutorial

In the previous article, we have studied the functions and arrays, Now we are moving towards objects and classes in python.

Lets start…

Objects and Classes in Python Introduction

Python is an object-oriented programming language. The Python Objects are a combination of variables and functions into a single entity (or single unit). 

A Class is work as an object constructor, without classes. It is a “blueprint” for creating objects.

How to Create a Class in Python?

For creating a class, we have to use the keyword class:

class Classname:
  x = 5

Creating Objects

Now we can use the class named Classname to create a object:

Example.1:

a1 = Classname()
print(a1.x)
Example.2:
class Person:             
    pass                    
p1 = Person()      # Creating the object 'p1'
print(p1)            # -> '<__main__.Person object at 0x0A...>'

The output of print on object p1, tell you what class it belongs to and hints on memory address it is referenced to.

Setting Attributes

  • You can set attributes, one a time, to an instantiated object and access it using the dot notation.
  • The value which is set to an attribute can be anything: a Python primitive, a built-in data type, another object. It can even be a function or a class.

Example

class Person:
    pass
p1 = Person()
p1.fname = 'Joe'
p1.lname = 'Simmons'
print(p1.fname, '-', p1.lname)  # -> 'Jack - Simmons'
Output:
objects and classes in python
  • We can also set multiple attributes, at once, by defining the initializer method, __init__, inside the class.
  • This method is called by default, during object creation.
  • It takes values passed inside the parenthesis, during object creation, as it’s arguments.
  • It also takes self as the first argument, which refers to the current object.

Setting Attributes Example

  • In the below-mentioned example, Person class sets two attributes using __init__ method.
class Person:
    def __init__(self, fname, lname):
        self.fname = fname
        self.lname = lname
p1 = Person('Demon', 'Stefen')   
print(p1.fname, '-', p1.lname)  

Output:

objects and classes in python

Documenting a Class

Each class or a method definition can have an optional first line, this is known as the docstring.

Example

class Person:
    'Class Person Represents a person.'
    def __init__(self, fname, lname):
        'Initialises two attributes of a person.'
        self.fname = fname
        self.lname = lname

Understanding a Class

Once documented, we can load the script into an interactive interpreter and run the help command on Person class.

>>>help(Person)

Help on class Person in module __main__:


class Person(builtins.object)

 |  Represents a person.

 |  

 |  Methods defined here:

 |  

 |  __init__(self, fname, lname)

 |      Initialises two attributes of a person.

 |  

... and more

Modifying the object

We can modify the properties on objects like mentioned example:

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

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

p1 = Person("Joe", 36)

p1.name= 'Demon'

print(p1.name)

Delete Object Properties

We can also delete the properties on objects by using the del keyword:

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

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

p1 = Person("Joe", 26)

del p1.age

print(p1.age)

Output should be:

objects and classes in python

FAQs:

What signifies an entity in the real world with its identity and behavior?
An Object.
For what setattr() used ?
To set an attribute we can use setattr().
What is Instantiation in the terms of OOP terminology?
For Creating an instance of class
The getattr() used for….
To access the attribute of the object
Non-static is not a class method. True or False
True.
What are the methods that begin and end with two underscore characters?
The Special methods are( like __init__ )begin and end with two underscore characters.

Must read Previous article:


In the above, we have presented the objects and classes in python and some other queries related to this topic with examples. I hope the concept of Oops will be clear after this tutorial.

Thank You 🙂

Leave a Reply

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