{"id":327,"date":"2021-03-15T21:14:00","date_gmt":"2021-03-15T21:14:00","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/?page_id=327"},"modified":"2021-06-25T11:32:22","modified_gmt":"2021-06-25T10:32:22","slug":"records","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/higher\/records\/","title":{"rendered":"Records"},"content":{"rendered":"<p>Python dataclasses are very similar to records &#8211; a single object with several named attributes.<\/p>\n<p><em>Note: dataclasses require Python 3.7+<\/em><\/p>\n<h1>Defining a Record<\/h1>\n<ul>\n<li>Import commands should be at the very top of any program, followed by any constants.<\/li>\n<li>Dataclass definitions come next, before any subprograms.<\/li>\n<li>The @dataclass\u00a0<em>decorator<\/em> is required before each dataclass definition, otherwise Python will try to use standard class definitions<\/li>\n<li>Names of dataclasses should start with a capital letter<\/li>\n<\/ul>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#\r\nfrom dataclasses import dataclass\r\n#\r\n@dataclass\r\nclass Book:\r\n    title: str = &quot;&quot;\r\n    year : int = 0\r\n    cost : float = 0.0\r\n    outofPrint : bool = False\r\n#<\/pre>\n<h1>Creating New Records<\/h1>\n<h2>Method 1<\/h2>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#\r\nnewBook = Book()\r\nnewBook.title= &quot;XYZ&quot;\r\nnewBook.year = 2001\r\nnewBook.price = 14.99\r\nnewBook.outOfPrint = False\r\n#<\/pre>\n<h2>Method 2<\/h2>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#\r\nnewBook = Book(&quot;XYZ&quot;, 2001, 14.99, False)\r\n#<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Python dataclasses are very similar to records &#8211; a single object with several named attributes. Note: dataclasses require Python 3.7+ Defining a Record Import commands should be at the very top of any program, followed by any constants. Dataclass definitions come next, before any subprograms. The @dataclass\u00a0decorator is required before each dataclass definition, otherwise Python will try to use standard class definitions Names of dataclasses should start with a capital<\/p>\n<p><a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/higher\/records\/\">Read More<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"parent":330,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-327","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/327","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=327"}],"version-history":[{"count":13,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/327\/revisions"}],"predecessor-version":[{"id":719,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/327\/revisions\/719"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/330"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/media?parent=327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}