Attributes of a class

Date: February 13th 2016
Last updated: February 13th 2016

You can access attributes of a class using pythons special methods.

# create empty class
class Testing(object): 
     """Testing module"""
     pass

#instantiate class
att = Testing()

#print object identity 
att.__class__
#<class '__main__.Testing'>

#print the class name
att.__class__.__name__
#'Testing'

# print the docstring
att.__doc__
#'Testing module'

# add attributes to instantiated class object
att.x = 'a'
att.y = 123
att.z = [1,2,3]

# print the dictionary that holds these attributes
att.__dict__
#{'z': [1, 2, 3], 'y': 123, 'x': 'a'}

results matching ""

    No results matching ""