{"id":696,"date":"2021-06-24T15:23:31","date_gmt":"2021-06-24T14:23:31","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/?page_id=696"},"modified":"2021-06-25T13:53:27","modified_gmt":"2021-06-25T12:53:27","slug":"array-of-objects","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/adv-higher\/array-of-objects\/","title":{"rendered":"Array of Objects"},"content":{"rendered":"<p>Working with arrays of objects is very similar to working with <a href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/higher\/arrays-of-records\/\">arrays of records<\/a>, but using constructors and methods instead of accessing record attributes directly.<\/p>\n<h1>Creating Arrays of Records<\/h1>\n<h2>Method 1a (SQA Arrays)<\/h2>\n<p>This creates an array of None types as placeholders.<\/p>\n<p>These are then replaced by objects using the <strong>init<\/strong> constructor method.<\/p>\n<pre class=\"brush: python; title: Python; notranslate\" title=\"Python\">\r\n#\r\ncomputers = &#x5B;None for c in range(20)]\r\n#\r\ncomputers&#x5B;0] = Computer('AHS1913013','Desktop','Windows Vista')\r\n#<\/pre>\n<h2>Method 1b (SQA Arrays)<\/h2>\n<p>This requires a constructor method that uses default values for attributes, and setter methods to give each attribute a value.<\/p>\n<pre class=\"brush: python; title: Python; notranslate\" title=\"Python\">\r\n#\r\ncomputers = &#x5B;Computer() for c in range(20)]\r\n#\r\ncomputers&#x5B;0].setID = 'AHS1913013'\r\ncomputers&#x5B;0].setType = 'Desktop'\r\ncomputers&#x5B;0].setOS = 'Windows Vista'\r\n#<\/pre>\n<h2>Method 2 (Python Lists)<\/h2>\n<p>Use constructor methods to add new objects to a list:<\/p>\n<pre class=\"brush: python; title: Python; notranslate\" title=\"Python\">\r\n#\r\ncomputers = &#x5B;]\r\n#\r\ncomputers.append(Computer('AHS1913013','Desktop','Windows Vista'))\r\ncomputers.append(Phone('XYZ1234567','Android 8','EE'))\r\n<\/pre>\n<h1>Traversing an Array of Objects<\/h1>\n<p>Do not access, or alter, attribute values directly. User getter and setter methods instead.<\/p>\n<h2>Method 1 &#8211; by index<\/h2>\n<pre class=\"brush: python; highlight: [2,4]; title: Python; notranslate\" title=\"Python\">\r\nfor c in range(len(computers)):\r\n    str = computers&#x5B;c].getID()\r\n    str += &quot; is a &quot;\r\n    str += computers&#x5B;c].getType()\r\n    print(str)\r\n<\/pre>\n<h2>Method 2 &#8211; by object<\/h2>\n<pre class=\"brush: python; highlight: [2,4]; title: Python; notranslate\" title=\"Python\">\r\nfor computer in computers:\r\n    str = computer.getID()\r\n    str += &quot; is a &quot;\r\n    str += computer.getType()\r\n    print(str)    \r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Working with arrays of objects is very similar to working with arrays of records, but using constructors and methods instead of accessing record attributes directly. Creating Arrays of Records Method 1a (SQA Arrays) This creates an array of None types as placeholders. These are then replaced by objects using the init constructor method. # computers = &#x5B;None for c in range(20)] # computers&#x5B;0] = Computer(&#8216;AHS1913013&#8242;,&#8217;Desktop&#8217;,&#8217;Windows Vista&#8217;) # Method 1b (SQA<\/p>\n<p><a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/adv-higher\/array-of-objects\/\">Read More<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"parent":291,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-696","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/696","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/comments?post=696"}],"version-history":[{"count":6,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/696\/revisions"}],"predecessor-version":[{"id":725,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/696\/revisions\/725"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/291"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/media?parent=696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}