0% found this document useful (0 votes)
7 views11 pages

1.8 Objects & Methods

This document provides an introduction to objects and methods in Python, explaining that all data in Python is represented as objects with unique identities, types, and values. It discusses the use of the 'is' operator to compare object identities and the distinction between object equality and value equality. Additionally, it covers the object() function, its properties, and how methods are invoked on objects.

Uploaded by

Bhargav Rajyagor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views11 pages

1.8 Objects & Methods

This document provides an introduction to objects and methods in Python, explaining that all data in Python is represented as objects with unique identities, types, and values. It discusses the use of the 'is' operator to compare object identities and the distinction between object equality and value equality. Additionally, it covers the object() function, its properties, and how methods are invoked on objects.

Uploaded by

Bhargav Rajyagor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Unit – 1

Introduction to Python
1.8 Objects & Methods
Object

Objects are Python’s abstraction for data.


All data in a Python program is represented by objects or by relations between objects. (In a sense, and in conformance to
Von Neumann’s model of a “stored program computer”, code is also represented by objects.)


Every object has an identity, a type and a value. An object’s identity never changes once it has been created; you may think
of it as the object’s address in memory. The ‘is’ operator compares the identity of two objects; the id() function returns an
integer representing its identity.


In Python, all data—including numbers and strings—are actually objects


In Python, a number is an object, a string is an object, and every datum is an object.


Objects of the same kind have the same type.


You can use the id function and type function to get these pieces of information about an object.

2
Object

>>> s = "Welcome" # s is a string


For example, >>> f = 3.0 # f is a float
>>> id(s)
>>> n = 3 # n is an integer >>> id(f)
36201472
>>> id(n) 26647120
>>> type(s)
505408904 >>> type(f)
<class 'str'>
>>> type(n) <class 'float'>

<class 'int'>

3
Object
The id for the object is automatically assigned a unique integer by Python when the program
is executed. The id for the object will not be changed during the execution of the program.

However, Python may assign a different id every time the program is executed. The type for
the object is determined by Python according to the value of the object. In Python, an
object’s type is defined by a class.

4
‘is’ operator with Object
# Define a list and assign it to two different variables

The result of the is operator is True if the objects are the
list1 = [1, 2, 3]
same and False otherwise.
list2 = list1

# Check if the two variables point to the same object



In this example, list1 and list2 refer to the same object
because both variables are assigned the same list. So, list1
print(list1 is list2) # Output: True
is list2 returns True.
# Create a new list with the same elements as list1

list3 = [1, 2, 3] 
However, list1 and list3 are two separate lists, even though
they contain the same elements. Therefore, list1 is list3
# Check if list1 and list3 point to the same object
returns False.
print(list1 is list3) # Output: False

# Check if list1 and list3 have the same content (values) 


It's important to understand that the is operator is different
from the equality (==) operator. The == operator checks if
print(list1 == list3) # Output: True
the values of the objects are equal, whereas the is operator
checks if the objects themselves are the same in terms of
memory location.

5
Methods for an object
The operations are defined using functions. The functions for the objects are called methods in Python. Methods can only be invoked

from a specific object. For example, the string type has the methods such as lower() and upper(),which return a new string in lowercase

and uppercase. Here are some examples of how to invoke these methods:

>>> s = "Welcome"

>>> s1 = s.lower() # Invoke the lower method

>>> s1

'welcome'

>>> s2 = s.upper() # Invoke the upper method As you can see from the preceding example, the syntax to invoke

>>> s2 a method for an object is object.method().

'WELCOME'

6
Python object() Method
The Python object() function returns the empty object, and the Python object takes no parameters.

For versions of Python 3.x, the default situation. The base class for all classes, including user-defined ones, is the Python object class. As

a result, in Python, all classes inherit from the Object class.

Syntax : obj = object()

Parameters : None

Returns : Object of featureless class. Acts as base for all object

7
Python object()
he object() function was used to create a new object, and the type() and dir() methods were used to identify the object’s characteristics.

We can see from the results that obj is a part of the object class. We can also observe that there is no __dict__ attribute on obj. As a

result, we are unable to give an instance of the object class attributes.

# declaring the object of class object

obj = object()
OUTPUT
# printing its type The type of object class object is :
The attributes of its class are :
print("The type of object class object is: ") [‘__class__’, ‘__delattr__’, ‘__dir__’, ‘__doc__’, ‘__eq__’, ‘__format__’, ‘__ge__’,
‘__getattribute__’, ‘__gt__’, ‘__hash__’, ‘__init__’, ‘__le__’, ‘__lt__’, ‘__ne__’, ‘__new__’,
print(type(obj)) ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__setattr__’, ‘__sizeof__’, ‘__str__’,
‘__subclasshook__’]
# printing its attributes

print("The attributes of its class are: ")

print(dir(obj))

8
Python object()
Properties of the object() function in Python


Objects of object class cannot add new attributes to it.


These objects are uniquely made and do not equate to one other, i.e don’t return true once compared.


the object acts as a base class for all the custom objects that we make.

9
Python object()

class example():
a = "Python"

# declaring the objects of class object


obj1 = example()
obj2 = example()

# checking for object equality


print("Is obj1 equal to obj2 : " + str(obj1 == obj2))

# checking for subclass


print("The Example class is a subclass of the object class? ", issubclass(example, object))

# checking for object instance


print("The obj1 is a instance of the object class? ", isinstance(obj1, object))

# trying to add attribute to object OUTPUT


print("Default attribute: ", obj1.a) Is obj1 equal to obj2 : False
obj1.a = "Python for AI" The Example class is a subclass of the object class? True
print("Assigning new attribute: ", obj1.a) The obj1 is a instance of the object class? True
Default attribute: Python
10
Assigning new attribute: Python for AI
Thanks

You might also like