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.
getattr(object, name, default value)
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)
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 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 email@example.com.
Please write your suggestion in comment section below if you find anything incorrect in this blog post