DigitMagazine

Example function call syntax and outputs in Python 3

Aug 25th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. Example functions:
  2. print (hasattr(emp1, 'location')) # Returns true if 'location' attribute exists
  3. print (getattr(emp1, 'name’)) # Returns value of 'name' attribute
  4. print (setattr(emp2, 'location', ‘Delhi’)) # Set attribute 'location' to Delhi (note single
  5. quotes are used when specifying the value)
  6. print (delattr(emp1, 'empid')) # Delete attribute 'empid’
  7.  
  8. Respective outputs
  9.  
  10. False - for hasattr(emp1, 'location')
  11. 1104 - for getattr(emp1, 'name’)
  12. None - for setattr(emp2, 'location', ‘Delhi’)
  13. None - for delattr(emp1, 'empid')
Add Comment
Please, Sign In to add comment