Python OOPs |In-built class functions

Python In-built class functions

The in-built functions defined in the class are described in the following table.

1. getattr(obj,name,default)
It is used to access the attribute of the object.

2. setattr(obj, name,value)
It is used to set a particular value to the specific attribute of an object.

3. delattr(obj, name)
It is used to delete a specific attribute.

4. hasattr(obj, name)
It returns true if the object contains some specific attribute.

getattr

syntax:
getattr(obj,name,default)
It is used to access the attribute of the object.

example:

#class creation
class employee:
    def __init__(self,e,n,s):
        self.empno=e
        self.name=n
        self.sal=s
    def show(self):
        print("Empno ",self.empno)
        print("Name ",self.name)
        print("Salary ",self.sal)

#calling
#parameterized
e=employee(5001,"Kapil",9800)
print("\nDisplay all the details")
e.show()

#prints the attributes  of the object e
print("\nExample of getattr  ")
print("Empno ",getattr(e,'empno'))
print("Name ",getattr(e,'name'))
print("Salary ")
print(getattr(e,'sal'))

Output:

Display all the details
Empno  5001
Name  Kapil
Salary  9800

Example of getattr  
Empno  5001
Name  Kapil
Salary 
9800
>>> 

setattr

Syntax:
setattr(obj, name,value)
It is used to set a particular value to the specific attribute of an object.

Example:

#class creation
class employee:
    def __init__(self,e,n,s):
        self.empno=e
        self.name=n
        self.sal=s
    def show(self):
        print("Empno ",self.empno)
        print("Name ",self.name)
        print("Salary ",self.sal)

#calling
#parameterized
e=employee(5001,"Kapil",9800)
print("\nDisplay all the details")
e.show()

setattr(e,'name',"Amit")
setattr(e,'sal','7600')

#prints the attributes  of the object e
print("\nExample of getattr  ")
print("Empno ",getattr(e,'empno'))
print("Name ",getattr(e,'name'))
print("Salary ")
print(getattr(e,'sal'))

Output:

Display all the details
Empno  5001
Name  Kapil
Salary  9800

Example of getattr  
Empno  5001
Name  Amit
Salary 
7600
>>> 

delattr

Syntax:
delattr(obj, name)
It is used to delete a specific attribute.

Example:

#class creation
class employee:
    def __init__(self,e,n,s):
        self.empno=e
        self.name=n
        self.sal=s
    def show(self):
        print("Empno ",self.empno)
        print("Name ",self.name)
        print("Salary ",self.sal)

#calling
#parameterized
e=employee(5001,"Kapil",9800)
print("\nDisplay all the details")
e.show()

delattr(e,'name')

#prints the attributes  of the object e
print("\nExample of getattr  ")
print("Empno ",getattr(e,'empno'))
print("Salary ")
print(getattr(e,'sal'))
print("Name ",getattr(e,'name'))

Output:

Error gets generated as name attribute is deleted.

Display all the details
Empno  5001
Name  Kapil
Salary  9800

Example of getattr  
Empno  5001
Salary 
9800
Traceback (most recent call last):
  File "C:/Python37/cla2.py", line 25, in <module>
    print("Name ",getattr(e,'name'))
AttributeError: 'employee' object has no attribute 'name'
>>> 

hasattr

Syntax:
hasattr(obj, name)
It returns true if the object contains some specific attribute.

Example:

#class creation
class employee:
    def __init__(self,e,n,s):
        self.empno=e
        self.name=n
        self.sal=s
    def show(self):
        print("Empno ",self.empno)
        print("Name ",self.name)
        print("Salary ",self.sal)

#calling
#parameterized
e=employee(5001,"Kapil",9800)
print("\nDisplay all the details")
e.show()

print("To check attribute is present or not")
print("Emplno is present  ",hasattr(e,'empno'))
print("Name is present  ",hasattr(e,'name'))
print("Salary is present  ",hasattr(e,'sal'))
print("Address is present  ",hasattr(e,'address'))

Output:

Display all the details
Empno  5001
Name  Kapil
Salary  9800
To check attribute is present or not
Emplno is present   True
Name is present   True
Salary is present   True
Address is present   False
>>> 

Built-in class attributes

Along with the other attributes, a python class also contains some built-in class attributes which provide information about the class.Built-in class attributes are given:

1.
__dict__
It provides the dictionary containing the information about the class namespace.

2.
__doc__
It contains a string which has the class documentation

3.
__name__
It is used to access the class name.

4.
__module__
It is used to access the module in which, this class is defined.
5.
__bases__
It contains a tuple including all base classes.

class Student:
    """ This is class student
    """
    def __init__(self,roll,name,per):  
        self.roll = roll;
        self.name = name;  
        self.per = per  
    def display_details(self):  
        print("Roll no %d Name:%s, Per:%d"%(self.roll,self.name,self.per))
#calling
s = Student(101,"Amit",99)  
print(s.__doc__)  
print(s.__dict__)  
print(s.__module__)  

Output:

This is class student
    
{'roll': 101, 'name': 'Amit', 'per': 99}
__main__
>>>