Python getattr() function:
Returns the value of the named attribute of an object.
Returns the default value provided to the function, if named attribute not found.
Syntax:
getattr(object, name, default value)
Example:
class Student:
student_id=""
student_name=""
# initial constructor to set the values
def __init__(self):
self.student_id = "101570"
self.student_name = "Naveen kumar"
student = Student()
# get attribute values by using getattr() function
print('\ngetattr : name of the student is =', getattr(student, "student_name"))
But you can access it by this
# but you could access this like this
print('traditional: name of the student is =', student.student_name)
Output:
getattr : name of the student is = Naveen kumar
traditional: name of the student is = Naveen kumar
For other top rated Codersarts python program links
For given latitude value check the location on equator in python
Calculate distance between two points on Earth the latitute and longitude given in degrees
Write a program of Rational Numbers Addition,Subtraction, Multiplication, Division in python
Design and implement a "Golf Club" program that records information about golfers and tournaments
Concatenate First, mid and last character of String in Python
A set of utilities to manipulate polynomials, Addition, subtraction, Multiplication and evaluation
For other top rated python program visit here
If you like Codersarts blog and looking for Assignment help,Project help, Programming tutors help and suggestion you can send mail at contact@codersarts.com.
Please write your suggestion in comment section below if you find anything incorrect in this blog post