Here's an example of a class with a dictionary attribute in Python:
main.py65 chars4 lines
In this class, my_dict
is an attribute that is set to an empty dictionary in the constructor (__init__
) using self.my_dict = {}
. You can then access and modify this dictionary attribute using the instance of MyClass
.
For example, you can add a key-value pair to the dictionary like this:
main.py67 chars3 lines
And you can access the value associated with "my_key"
like this:
main.py59 chars2 lines
gistlibby LogSnag