get object attributes python code example

Example 1: get object attributes python

for att in dir(your_object):
    print (att, getattr(your_object,att))

Example 2: python get object attribute by string

x = getattr(t, 'attr1')
setattr(t, 'attr1', 21)

Example 3: python get attributes of object

getattr(object, 'attribute_name')

Example 4: python get attributes of class

import inspect

class myclass:
  a = 5
  def method(b):
    return b

for i in inspect.getmembers(myclass):
  print(i)

Example 5: python get attributes of object

field_name = "fullName"
print getattr(user, field_name) # prints content of user.fullName