create an object with a dictionnary and a string as attributes in python

To create an object with a dictionary and a string as attributes in Python, create a class with the desired attributes and their types. In the __init__ method, set the dictionary and string attributes to their initial values. Here's an example:

main.py
class MyClass:
    def __init__(self, my_dict, my_string):
        self.my_dict = my_dict
        self.my_string = my_string
125 chars
5 lines

You can then create instances of this class and access their attributes like this:

main.py
my_dict = {'key1': 'value1', 'key2': 'value2'}
my_string = 'hello world'
my_obj = MyClass(my_dict, my_string)

print(my_obj.my_dict)    # Outputs: {'key1': 'value1', 'key2': 'value2'}
print(my_obj.my_string)  # Outputs: hello world
232 chars
7 lines

In the example above, my_obj is an instance of the MyClass class with a dictionary attribute my_dict and a string attribute my_string.

gistlibby LogSnag