|
1113 | 1113 | "\n", |
1114 | 1114 | "print(ip4_valid_refs.serialize(pretty=True))" |
1115 | 1115 | ] |
| 1116 | + }, |
| 1117 | + { |
| 1118 | + "cell_type": "markdown", |
| 1119 | + "metadata": {}, |
| 1120 | + "source": [ |
| 1121 | + "## Immutability Issues\n", |
| 1122 | + "\n", |
| 1123 | + "It's been shown above that changing an object's properties will trigger an ImmutableError, but because List and Dictionary properties retain their built-in content-modification functions, it is possible to modify some of an object's data. ImmutableError's are triggered when the object ID of a field is changed. Strings are inherently immutable, so any attempt to modify their content results in a new object being created - with a new ID - triggering the ImmutableError. But using the content-modification functions of Lists and Dictionaries does not change the underlying object ID, and therefore, doesn't trigger the error.\n", |
| 1124 | + "\n", |
| 1125 | + "This does violate the design principle of STIX 2 being immutable by default, but it is something to be aware off, if only to avoid doing it by mistake. It also allows the possibility of complex objects being built up by appending to fields, rather than creating objects all in one action.\n", |
| 1126 | + "\n", |
| 1127 | + "Here an Identity object is created. Fields like 'name' and 'identity_class' are immutable, but the 'roles' data can be modified using the List operations 'clear' and 'append'." |
| 1128 | + ] |
| 1129 | + }, |
| 1130 | + { |
| 1131 | + "cell_type": "code", |
| 1132 | + "execution_count": null, |
| 1133 | + "metadata": {}, |
| 1134 | + "outputs": [], |
| 1135 | + "source": [ |
| 1136 | + "from stix2 import Identity\n", |
| 1137 | + "\n", |
| 1138 | + "identity1 = Identity(\n", |
| 1139 | + " x_custom=\"IRF system\",\n", |
| 1140 | + " name=\"IRF\",\n", |
| 1141 | + " description=\"Incident Reporting Form (IRF) system\",\n", |
| 1142 | + " identity_class=\"system\",\n", |
| 1143 | + " roles=[\"director\"],\n", |
| 1144 | + " sectors=[\"commercial\", \"defense\"],\n", |
| 1145 | + ")\n", |
| 1146 | + "\n", |
| 1147 | + "identity1.roles.clear()\n", |
| 1148 | + "identity1.roles.append(\"cleared-dod-contractor\")\n", |
| 1149 | + "\n", |
| 1150 | + "print(identity1.serialize(pretty=True))" |
| 1151 | + ] |
1116 | 1152 | } |
1117 | 1153 | ], |
1118 | 1154 | "metadata": { |
|
0 commit comments