{"assignment":{"_schema_version":2,"course_id":37,"date_created":"2022-06-28T19:00:00+00:00","date_modified":"2023-09-10T13:09:43.393793+00:00","extra_instructor_files":"","extra_starting_files":"","forked_id":null,"forked_version":null,"hidden":false,"id":1035,"instructions":"## Primitives Types\n\n* Boolean\n* Integer\n* Float\n* String\n* None\n\nWe have learned about 5 primitive types: Booleans, Integers, Floats, Strings, and the special None.\nWe can use these to represent many kinds of data, but they have a limitation since they are so simple and cannot be combined with each other.\nTo increase our capabilities, we will learn about a new category of types, which is Composite types.\nComposite types are named because they are composed of other types, unlike primitive types.\nThe first kind of composite types we will learn about are Dataclasses.\n\n## Dataclasses\n\n- **Dataclass:** A way to make new types that collect multiple values together in one place.\n\nDataclasses are a data structure that lets us combine other types together to make a new type.\nYou should not think of Dataclass as a type itself; instead, it is _a way of creating new types_.\nOr put another way, dataclasses are a _kind_ of type, without being a type themselves.\n\n## An Example Dataclass\n\n```python dataclass-example\n# Required import\nfrom dataclasses import dataclass\n\n# Definition\n@dataclass\nclass Box:\n    width: int\n    length: int\n\n# Instance creation\nmy_box = Box(5, 10)\n# Using the instance\nprint(my_box)\n```\n\nLet's look at an example Dataclass.\nFirst, we must import a decorator from the built-in `dataclasses` module (one of the many built-in libraries that Python conveniently provides for us.) Then we create a definition for the dataclass by writing the header and body of the dataclass.\n\nThe header requires the decorator, the `class` keyword, the name of the new dataclass, and a colon.\nThe body requires that we specify the fields, each on their own line.\nOnce defined, we can create _instances_ of the dataclass using the _constructor_ function, which has the same name as the defined dataclass.\nStoring the result in a variable allows us to use the instance later, such as shown  in the following example where we print the `my_box` variable.\nLet's break down each part of this example further!\n\n## @dataclass Decorator\n\n```python\nfrom dataclasses import dataclass\n\n@dataclass\nclass Dog:\n  pass\n```\n\nPerhaps the most confusing part of dataclasses is the _decorator_.\nThe decorator is the `@dataclass` annotation.\nThe \"at\" symbol that we see in front of the word \"dataclass\" is the syntax that makes this a decorator.\nBasically, a decorator adds extra functionality on top of classes \u2014 in this case to make the classes into dataclasses.\nThe decorated dataclasses are far more powerful and convenient than regular classes.\n\nThere's a lot that can go wrong when using a decorator.\nYou must `import` the `dataclass` decorator from the `dataclasses` module without including the @ symbol.\nHowever, when you put the decorator above the class definition, you absolutely must include the @ symbol.\nMessing up either part will prevent Python from understanding that you want to create a dataclass.\n\n## Dataclass Fields\n\n```python dataclass-fields\nfrom dataclasses import dataclass\n\n@dataclass\nclass Dog:\n    name: str\n    age: int\n    is_fuzzy: bool\n```\n\nThe fields of a dataclass are the most useful part because they allow us to bundle up related data into a single place.\nEach field is like a variable inside of the dataclass.\nThe rules for naming fields are the same as the rules for naming variables.\nFirst, the name must have only letters, digits, and underscores.\nSecond, the name must only begin with letters or underscores.\nThe rules for specifying the type of fields are like the rules for specifying the type of function parameters, except instead of commas, each field is on its own line.\n\n## The Constructor Function\n\n```python dataclass-constructor\nfrom dataclasses import dataclass\n\n@dataclass\nclass Dog:\n    name: str\n    age: int\n    is_fuzzy: bool\n\nada = Dog(\"Ada Bart\", 5, True)\n```\n\nOnce a dataclass is defined, we can create any number of instances that represent concrete versions of the original dataclass.\nInstances of a dataclass are created by calling the constructor function.\nThe _constructor_ is a special function created by the dataclass definition and given the same name.\nThe similarity between defining a function and defining a dataclass is very strong.\nThe similarity between calling a function and _instantiating_ (\"creating an instance of\") a dataclass is even stronger.\nEach field of the dataclass definition corresponds to an argument for the constructor.\nThe order and type of each field must match the order and type of each argument.\n\n## Dataclass vs Instance\n\n- **Dataclass:** The pattern that is used as a base for instances\n- **Instance:** A specific, concrete example of a class\n\nA very confusing concept is what a dataclass represents compared to an instance of the dataclass.\nA similar concept is the difference between a stencil and a drawing.\nAnother similar concept is the difference between a recipe and a cake.\nIn all these cases, one of these is an _idea_, something totally theoretical.\nThe other is a concrete thing.\n\n## Dog vs Instance of Dog\n\n![A diagram of an abstract dog sketch with a Dog dataclass definition, pointing to two concrete Dog instances that have photos of actual dogs.](bakery_structures_dataclasses_class_instance.png)\n\nLet us consider the `Box` example from before.\nWhen you only look at the definition of the `Box`, it is impossible to say what color a Box is.\nYou can generally say that the color of a Box will be a string value.\nBut there is no specific string value associated with the idea of a `Box`.\nInstead, you would need to create an instance of a Box to talk meaningfully about the color of that box.\nThe concept of a Box does not have a color or a length.\nYou can create an instance using the constructor function that is `\"red\"` or `\"blue\"`, but the original `Box` object itself does not inherently have a color.\nThe idea for dogs shown here is similar, with a distinction between the idea of a `Dog` and the specific values of real dogs.\nThere is no image for the abstract dataclass definition of a dog, other than a vague outline.\nHowever, the two real dog instances can have photos associated, because they model actual real dogs.\n\n## Accessing Fields\n\n```python dog-attributes\nfrom dataclasses import dataclass\n\n@dataclass\nclass Dog:\n    name: str\n    age: int\n    is_fuzzy: bool\n\nada = Dog(\"Ada Bart\", 4, True)\nprint(\"Ada's name is\", ada.name)\n```\n\nOne of the most critical parts of using an instance is to access its _fields_.\nThe period and the name of the field are used to access those fields.\nGenerally, you can think of a field as a variable that lives inside of an instance.\nIn the code shown here, we access the `name` field of the `Dog` instance stored in `ada`.\nA critical thing to understand, however, is that _you can only access the fields of an instance, not a dataclass_.\nIn other words, you cannot try to access the fields of the `Dog` dataclass, only the `ada` instance.\nIf you try changing the code, you will see that you get an error when you try to access `Dog.name` instead of `ada.name`.\nIt's very important to understand the idea that the `Dog` is an abstract idea, while the `ada` instance is a real concrete bit of data.\n\n## Terminology of Fields\n\n* Field\n* Attribute\n* Property\n* Member\n\nThere are many words that mean roughly the same thing when it comes to dataclasses.\nOne concept that has many synonyms is the term _field_, which can be used interchangeably with _attribute_, _property_, and _member_.\nWe will use the terms **field** and **attribute** most often, although occasionally we will refer to properties or members.\n\n## Tracing Dataclasses\n\n![A diagram labelled Variables and Values, above a snippet of code. The code is two lines long, with the first line being a call to the constructor in order to create an instance of a Dog dataclass and store it in the variable `ada`. The second line of code is an assignment of the Dog instance's `name` field's value to a `my_dogs_name` variable. The diagram models the representation in memory, including an arrow pointing from the `ada` variable to the Dog Instance on the right hand side of Values.](bakery_structures_dataclasses_tracing.png)\n\nThe data stored in a dataclass is more complicated than the data stored in a simple variable.\nThere are multiple pieces of data (the fields), all stored in a single object.\nConceptually, we say that the data for an instance lives in an area of memory called the _heap_, as opposed to living in the \"stack\" that we learned about in Unit 3.\nThe heap is a special region of memory with many interesting properties that we will explore over time.\nVariables on the stack can hold references to values on the heap.\nFor now, we can use a stack/heap diagram like the one below to visualize the relationship between the variables and data on the stack and the data on the heap.\n\nIn this stack/heap diagram, you can see how dataclasses are traced a little differently compared to regular primitive types of data.\nWhen the constructor is called, a new instance of a dataclass is created.\nThat instance is a special kind of value with multiple fields which can be seen on the right-hand side of the diagram.\nWhen code accesses one of those fields, like on the second line (`ada.name`), the arrow is followed and the named field's value is reached.\nThat value is a simple primitive string and so can be stored directly in the `my_dogs_name` variable.\nThe `ada` variable points to the entire `Dog` instance, unlike the `my_dogs_name` variable which points to a simple primitive value.\nFor now, all we need to do is keep track of the difference between primitive values stored in the stack and special instances stored on the heap.\nBut, eventually, we will develop a deeper understanding of how data is laid out.\n","ip_ranges":"","name":"4A1) Dataclasses Reading","on_change":"","on_eval":"","on_run":"","owner_id":1,"owner_id__email":"acbart@udel.edu","points":0,"public":true,"reviewed":false,"sample_submissions":[],"settings":"{\n  \"small_layout\": true,\n  \"header\": \"Dataclasses\",\n  \"slides\": \"bakery_structures_dataclasses.pdf\",\n  \"youtube\": {\n    \"Bart\": \"B-zRDEXIEMw\",\n    \"Amy\": \"uK-5uFYOzto\"\n  },\n  \"video\": {\n    \"Bart\": \"https://blockpy.cis.udel.edu/videos/bakery_structures_dataclasses-Bart.mp4\",\n    \"Amy\": \"https://blockpy.cis.udel.edu/videos/bakery_structures_dataclasses-Amy.mp4\"\n  }\n}","starting_code":"","subordinate":true,"tags":[],"type":"reading","url":"bakery_structures_dataclasses_read","version":11},"ip":"216.73.216.157","submission":{"_schema_version":3,"assignment_id":1035,"assignment_version":11,"attempts":0,"code":"","correct":false,"course_id":37,"date_created":"2026-05-20T16:04:46.923130+00:00","date_due":"","date_graded":"","date_locked":"","date_modified":"2026-05-20T16:04:46.923130+00:00","date_started":"","date_submitted":"","endpoint":"","extra_files":"","feedback":"","grading_status":"NotReady","id":2037041,"score":0.0,"submission_status":"Started","time_limit":"","url":"submission_url-e22ffdb1-88c3-4f5d-8057-01b6c39dd14e","user_id":2044700,"user_id__email":"","version":0},"success":true}
