id() and type() Function in Python


Everything is an Object

  • Everything means everything, including functions and classes (more on this later!)
  • Data type is a property of the object and not of the variable

id() Function:

id() is an inbuilt function in Python.

Syntax :

id(object)

Returns the identity of object. It is the address of object in memory.

It will be unique and constant throughout the lifetime of object.

Example :

>>> a = 10
>>> print("id of a",id(a))
id of a 1579869088
>>> b = "Hello World"
>>> print("id of b",id(b))
id of b 53798096
>>> 


type() Function:

type() is an inbuilt function in Python.

The type() function returns the type of the specified object

Syntax :

Different forms of type() function in Python are:
type(object)
type(name, bases, dict)

Example 1 :

If a single argument (object) is passed to type() built-in, it returns type of the given object.

>>> a = 10
>>> print("type of a is",type(a))
type of a is <class 'int'>
>>> b = 1.2
>>> print("type of b is",type(b))
type of b is <class 'float'>
>>> c = True
>>> print("type of c is",type(c))
type of c is <class 'bool'>
>>> d = [1,2,3]
>>> print("type of d is",type(d))
type of d is <class 'list'>
>>> e = (1,2,3)
>>> print("type of e is",type(e))
type of e is <class 'tuple'>

Example 2 :

type() With name, bases and dict Parameters

If three arguments (name, bases and dict) are passed, it returns a new type object.

Parameters:

name: name of class, which later corresponds to the __name__ attribute of the class. bases : tuple of classes from which the current class derives. Later corresponds to the __bases__ attribute. dict : a dictionary that holds the namespaces for the class. Later corresponds to the __dict__ attribute.

Return type:

returns a new type class or essentially a metaclass.

Use of type(name, bases, dict)

File name: checkType.py
# Python3 program to demonstrate 
# type(name, bases, dict) 

# New class(has no base) class with the 
# dynamic class initialization of type() 
new = type('New', (object, ), 
	dict(var1 ='ProwessApps', b = 2016))

# Print type() which returns class 'type' 
print(type(new)) 
print(vars(new)) 


# Base class, incorporated 
# in our new class 
class test: 
	a = "ProwessApps"
	b = 2016

# Dynamically initialize Newer class 
# It will derive from the base class test 
newer = type('Newer', (test, ), 
		dict(a ='Prowess', b = 2018))
		
print(type(newer)) 
print(vars(newer)) 
OUTPUT:
{'__module__': '__main__', 'var1': 'ProwessApps', '__weakref__': , 'b': 2016, '__dict__': , '__doc__': None}

{'b': 2018, '__doc__': None, '__module__': '__main__', 'a': 'Prowess'} 
                
type() function is mostly used for debugging purposes.

Example :

# Python3 simple code to explain 
# the type() function 
print(type([]) is list) 

print(type([]) is not list) 

print(type(()) is tuple) 

print(type({}) is dict) 

print(type({}) is not list) 
OUTPUT:
True
False
True
True
True

Next chapter Mutable / Immutable concept





 









Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved