Python Objects and Classes: The Building Blocks of OOP

thumb_up 1  ·  sell Python objects and classes, Defining classes in Python, Working with objects in Python

Python is a highly object-oriented language. In Python, each and every element in a Python program is an object of one or the other class. A number, string, list, dictionary etc. used in a program they are objects of corresponding built-in classes.

Example

 
num=20 print (type(num)) num1=55.50 print (type(num1)) s="TutorialsPoint" print (type(s)) dct={'a':1,'b':2,'c':3} print (type(dct)) def SayHello(): print ("Hello World") return print (type(SayHello))

When you execute this code, it will produce the following output −

<class 'int'>
<class 'float'>
<class 'str'>
<class 'dict'>
<class 'function'>

In Python, the Object class is the base or parent class for all the classes, built-in as well as user defined.

The class keyword is used to define a new class. The name of the class immediately follows the keyword class followed by a colon as follows −

class ClassName: 'Optional class documentation string' class_suite
  • The class has a documentation string, which can be accessed via ClassName.__doc__.

  • The class_suite consists of all the component statements defining class members, data attributes and functions.

Example

class Employee(object): 'Common base class for all employees' pass

Any class in Python is a subclass of object class, hence object is written in parentheses. However, later versions of Python don't require object to be put in parentheses.

class Employee: 'Common base class for all employees' pass

To define an object of this class, use the following syntax −

e1 = Employee()
 
 
 
The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.

Was this answer helpful?

Related Articles

description

Exploring Python's Key Characteristic

Python is a feature rich high-level, interpreted, interactive and object-oriented scripting language. This tutorial will list down some of…

arrow_forward
description

Comparing Python and C++

Both Python and C++ are among the most popular programming languages. Both of them have their advantages and disadvantages. In this…

arrow_forward
description

Creating a Python Hello World Program

This tutorial will teach you how to write a simple Hello World program using Python Programming language. This program will make use of…

arrow_forward
description

Python's Versatile Application Domains

Python is a general-purpose programming language. It is suitable for development of wide range of software applications. Over last few…

arrow_forward
description

Understanding the Python Interpreter

Python is an interpreter-based language. In a Linux system, Python's executable is installed in /usr/bin/ directory. For Windows, the…

arrow_forward
arrow_back « Back