Python is an object-oriented programming language. Everything is in Python treated as an object, including variable, function, list, tuple, dictionary, set, etc. Every object belongs to its class. For example - An integer variable belongs to integer class. An object is a real-life entity. An object is the collection of various data and functions that operate on those data. An object contains the following properties.
- State - The attributes of an object represents its state. It also reflects the properties of an object.
- Behavior - The method of an object represents its behavior.
- Identity - Each object must be uniquely identified and allow interacting with the other objects.
Let's understand the object in the aspect of classes.
The classes and objects are the essential key to the object-oriented programming. Classes are the blueprint of the object. Classes are used to bundling the data and functionality together. Each newly created class must have its object. Let's understand real-life example of class and object.
A human is a class which may have may attributes such as walking, sleeping, thinking, etc. Suppose we want to name and age of 100 humans, so we don't need to create a class for every person. We just need to instantiate the multiple objects of that perticular class.
The class contains the user-defined data structure that holds the own data members such as variables, constructs, and member functions, which can be accessed by creating an object of the class.
The syntax of creating a class is given below. The syntax of creating a class is given below.
Syntax:
The class keyword is used to define the class and the user-define class name replaces ClassName.
Creating an Object of class
The object is essential to work with the class attributes. Instantiate is a term used when we create the object of any class, and the instance is also referred to as an object. The object is created using the class name. The syntax is given below.
Syntax:
In the following example, we have created the object of Person class.
Example -
Output:
Explanation:
In the above code, we have created a Person class which consisted of two attributes age, name and display function. We created the object of person class called per . Using the object along with the .dot operator, we accessed the class function.
Everything is an object
An object is a fundamental building block of an object-oriented language. Integers, strings, floating point numbers, even arrays and dictionaries, are all objects. More specifically, any single integer or any single string is an object. The number 12 is an object, the string "hello, world" is an object, a list is an object that can hold other objects, and so on. You've been using objects all along and may not even realize it.
Objects have types
Every object has a type, and that type defines what you can do with the object. For example, the int
type defines what happens when you add something to an int, what happens when you try to convert it to a string, and so on.
Conceptually, if not literally, another word for type is class. When you define a class, you are in essence defining your own type. Just like 12
is an instance of an integer, and "hello world"
is an instance of a string, you
can create your own custom type and then create instances of that type. Each instance is an object.
Classes are just custom types
Most programs that go beyond just printing a string on the display need to manage something more than just numbers and strings. For example, you might be writing a program that manipulates pictures, like photoshop. Or, perhaps you're creating a competitor to iTunes and need to manipulate songs and collections of songs. Or maybe you are writing a program to manage recipes.
A single picture, a single song, or a single recipe are each an object of a particular type. The only difference is, instead of your object being a type provided by the language [eg: integers, strings, etc], it is something you define yourself.
An Object is an instance of a Class. A class is like a blueprint while an instance is a copy of the class with actual values. Python is object-oriented programming language that stresses on objects i.e. it mainly emphasizes functions. Objects are basically an encapsulation of data variables and methods acting on that data into a single entity.
Note: For more information, Python Classes and Objects
Understanding of Object
For a better understanding of the concept of objects consider an example, many of you have played CLASH OF CLANS, So let’s assume base layout as the class which contains all the buildings, defenses, resources, etc. Based on these descriptions we make a village, here the village is the object.
Syntax:
obj = MyClass[] print[obj.x]
Instance defining represent memory allocation necessary for storing the actual data of variables. Each time when you create an object of class the copy of each data variables defined in that class is created. In simple language we can state that each object of a class has its own copy of data members defined in that class.
Creating an Object
Python3
class
Cars:
def
__init__[
self
, m, p]:
self
.model
=
m
self
.price
=
p
Audi
=
Cars[
"R8"
,
100000
]
print
[Audi.model]
print
[Audi.price]
Output:
R8 100000
Working of the Program:
Audi = Cars[]:
- A block of memory is allocated on the heap. The size of memory allocated is decided by the attributes and methods available in that class[Cars].
- After the memory block is allocated, the special method __init__[] is called internally. Initial data is stored into the variables through this method.
- The location of the allocated memory address of the instance is returned to the object[Cars].
- The memory location is passed to self.
Accessing Class Member Using Object:
Variable and method of a class are accessible by using class objects or instances.
Syntax:
obj_name.var_name Audi.model obj_name.method_name[] Audi.ShowModel[]; obj_name.method_name[parameter_list] Audi.ShowModel[100];
Example 1:
Python3
class
Car:
vehicle
=
'car'
def
__init__[
self
, model]:
self
.model
=
model
def
setprice[
self
, price]:
self
.price
=
price
def
getprice[
self
]:
return
self
.price
Audi
=
Car[
"R8"
]
Audi.setprice[
1000000
]
print
[Audi.getprice[]]
Output:
1000000
Example 2:
Python3
class
Car:
vehicle
=
'Car'
def
__init__[
self
, model, price]:
self
.model
=
model
self
.price
=
price
Audi
=
Car[
"R8"
,
100000
]
BMW
=
Car[
"I8"
,
10000000
]
print
[
'Audi details:'
]
print
[
'Audi is a'
, Audi.vehicle]
print
[
'Model: '
, Audi.model]
print
[
'price: '
, Audi.price]
print
[
'\n BMW details:'
]
print
[
'BMW is a'
, BMW.vehicle]
print
[
'Model: '
, BMW.model]
print
[
'Color: '
, BMW.price]
print
[
"\nAccessing class variable using class name"
]
print
[Car.vehicle]
print
[Audi.vehicle]
print
[BMW.vehicle]
Output:
Audi details: Audi is a Car Model: R8 price: 100000 BMW details: BMW is a Car Model: I8 Color: 10000000 Accessing class variable using class name Car Car Car
Self Variable:
SELF is a default variable that contains the memory address of the current object. Instance variables and methods can be referred to by the self variable. When the object of a class is created, the memory location of the object is contained by its object name. This memory location is passed to the SELF internally, as SELF knows the memory address of the object, so the variable and method of an object is accessible. The first argument to any object method is SELF because the first argument is always object reference. This process takes place automatically whether you call it or not.
Example:
Python3
class
Test:
def
__init__[Myobject, a, b]:
Myobject.country
=
a
Myobject.capital
=
b
def
myfunc[abc]:
print
[
"Capital of "
+
abc.country
+
" is:"
+
abc.capital]
x
=
Test[
"India"
,
"Delhi"
]
x.myfunc[]
Output:
Capital of India is: Delhi
Note: For more information, refer to self in Python class
Deleting an Object:
Object property can be deleted by using the del keyword:
Syntax:
del obj_name.property
objects also can be deleted by del keyword:
Syntax:
del obj_name