0% found this document useful (0 votes)
42 views

Get A Dictionary From An Objects Fields

Uploaded by

vidya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Get A Dictionary From An Objects Fields

Uploaded by

vidya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Get a dictionary from an Objects Fields

In this article, we will discuss how to get a dictionary from object’s field i.e. how
to get the class members in the form of a dictionary. There are two approaches to
solve the above problem:

By using the __dict__ attribute on an object of a class and attaining the dictionary.
All objects in Python have an attribute __dict__, which is a dictionary object
containing all attributes defined for that object itself. The mapping of attributes
with its values is done to generate a dictionary.
By calling the in-built vars method, which is used to return __dict__ attribute of a
module, class, class instance, or an object.
#Method 1: To generate a dictionary from an arbitrary object using
__dict__attribute:
# class Animals is declared
class Animals:

# constructor
def __init__(self):

# keys are initialized with


# their respective values
self.lion = 'carnivore'
self.dog = 'omnivore'
self.giraffe = 'herbivore'

def printit(self):
print("Dictionary from the object fields\
belonging to the class Animals:")
# object animal of class Animals
animal = Animals()

# calling printit method


animal.printit()
# calling attribute __dict__ on animal
# object and printing it
print(animal.__dict__)
Output:

Dictionary from the object fields belonging to the class Animals:


{‘lion’: ‘carnivore’, ‘dog’: ‘omnivore’, ‘giraffe’: ‘herbivore’}

#Method 2: To generate a dictionary from an arbitrary object using an in-built vars


method:

# class A is declared
class A:

# constructor
def __init__(self):
# keys are initialized with
# their respective values
self.A = 1
self.B = 2
self.C = 3
self.D = 4

# object obj of class A


obj = A()

# calling vars method on obj object


print(vars(obj))
Output:

{'A': 1, 'B': 2, 'C': 3, 'D': 4}


Inheritance in Python
• Inheritance is the capability of one class to derive or inherit the properties
from another class. 
Benefits of inheritance
• It represents real-world relationships well.
• It provides reusability of a code. We don’t have to write the same code again
and again. Also, it allows us to add more features to a class without
modifying it.
• It is transitive in nature, which means that if class B inherits from another
class A, then all the subclasses of B would automatically inherit from class
A.
class Person(object):
def __init__(self, name):
self.name = name
def getName(self):
return self.name
def isEmployee(self):
return False
class Employee(Person):
def isEmployee(self):
return True
emp = Person("name1")
print(emp.getName(), emp.isEmployee())
emp = Employee("name2")
print(emp.getName(), emp.isEmployee())
Types of Inheritance in Python
• Types of Inheritance depends upon the number of child and parent classes
involved. There are four types of inheritance in Python:
Single Inheritance
Single inheritance enables a derived class to inherit properties from a single
parent class, thus enabling code reusability and the addition of new features to
existing
Multiple Inheritance:
• When a class can be derived from more than one base class this type of
inheritance is called multiple inheritance. In multiple inheritance, all the
features of the base classes are inherited into the derived class. 

class Mother:
mothername = ""
def mother(self):
print(self.mothername)
class Father:
fathername = ""
def father(self):
print(self.fathername)
class Son(Mother, Father):
def parents(self):
print("Father :", self.fathername)
print("Mother :", self.mothername)
s1 = Son()
s1.fathername = "RAM"
s1.mothername = "SITA"
s1.parents()
Multilevel Inheritance 
In multilevel inheritance, features of the base class and the derived class are further
inherited into the new derived class. This is similar to a relationship representing a
child and grandfather.

# Base class
class Grandfather:
def __init__(self, grandfathername):
self.grandfathername = grandfathername

# Intermediate class
class Father(Grandfather):
def __init__(self, fathername, grandfathername):
self.fathername = fathername

# invoking constructor of Grandfather class


Grandfather.__init__(self, grandfathername)
# Derived class
class Son(Father):
def __init__(self,sonname, fathername, grandfathername):
self.sonname = sonname
# invoking constructor of Father class
Father.__init__(self, fathername, grandfathername)
def print_name(self):
print('Grandfather name :', self.grandfathername)
print("Father name :", self.fathername)
print("Son name :", self.sonname)
# Driver code
s1 = Son('Prince', 'Rampal', 'Lal mani')
print(s1.grandfathername)
s1.print_name()
Hierarchical Inheritance:
When more than one derived classes are created from a single base this type of
inheritance is called hierarchical inheritance. In this program, we have a parent
(base) class and two child (derived) classes

class First:
def __init__(self):
self.x = 20
self.y = 10
class Second(First):
def findsum(self):
self.z = self.x + self.y
print("Sum is:", self.z)
class Third(First):
def findsub(self):
self.z = self.x - self.y
print("Subtraction is:", self.z)
obj1 = Second()
obj1.findsum()
Hybrid Inheritance:
• Inheritance consisting of multiple types of inheritance is called hybrid
inheritance.
 

You might also like