Javascript type objects in Python
To make a new JsObject
:
import jsobj
a = jsobj.JsObject({
"first_letter": True, # `A` is the first letter of the alphabet
"vowel": True, # `A` is a vowel
"alphabet_position": 1, # `A` is the first letter of the alphabet
"consonant": False, # `A` is not a consonant
"examples_of_use": ["apple", "alkali", "algebra", "alligator", "acid"] # `A` is used in all these words
"famous_example": "apple" # Out of all words, apple is the most well known that starts with `A`
})
To add an attribute:
a.addAttr("in_hello", False) # adds the attribute `in_hello` and sets it to boolean `False`
To change the value of an existing attribute:
a.setAttr("examples_of_use", ["apple", "alkali", "algebra", "alligator", "acid", "all"]) # changes the list of examples
To delete an attribute:
a.delAttr("consonant") # deletes the consonant attribute
To get the object:
a_object = a.getObj()
To add a method to the object (can also be used to refresh the object after the dictionary is changed):
# define your method as a normal function
def a_print():
print("A is for apple")
# add the attribute
a.addMethod(a_print)
# refresh the object
a_object = a.getObj()
# call the method (optional)
a_object.printA()
Made by these members of the Code Roller team C=