|
230 | 230 | "* A **function** is independent and not associated with any particular object. \n", |
231 | 231 | "* A **method** is a function that is associated with an object and can access and modify that object's internal data.\n", |
232 | 232 | "\n", |
233 | | - "For example, `Circle.area` is a function defined inside the class—it needs to be told explicitly which object to operate on. However, when you create an instance like `c = Circle(1)`, the expression `c.area` refers to a method—it is the same function, but now automatically linked (or **bound**) to the object `c`.\n", |
| 233 | + "For example, `Circle.area` is a function defined inside the class—it needs to be told explicitly which object to operate on. However, when you create an instance like `c = Circle(1)`, the <span style=\"hyphens: none\">expression</span> `c.area` refers to a method—it is the same function, but now automatically linked (or **bound**) to the object `c`.\n", |
234 | 234 | "\n", |
235 | 235 | "In other words, `Circle.area` and `c.area` are not the same: the first is a plain function, while the second is a bound method. Behind the scenes, the instance object `c` is automatically passed as the first argument to the function, making the following two calls equivalent:" |
236 | 236 | ] |
|
432 | 432 | "output_type": "stream", |
433 | 433 | "text": [ |
434 | 434 | "__dict__ for class:\n", |
435 | | - "{'__module__': '__main__', '__doc__': 'Circle class', 'pi': 3, '__init__': <function Circle.__init__ at 0x7db50ae3b9c0>, 'area': <function Circle.area at 0x7db50ae3bf60>, '__dict__': <attribute '__dict__' of 'Circle' objects>, '__weakref__': <attribute '__weakref__' of 'Circle' objects>}\n", |
| 435 | + "{'__module__': '__main__', '__doc__': 'Circle class', 'pi': 3, '__init__': <function Circle.__init__ at 0x704e4c174040>, 'area': <function Circle.area at 0x704e4c174220>, '__dict__': <attribute '__dict__' of 'Circle' objects>, '__weakref__': <attribute '__weakref__' of 'Circle' objects>}\n", |
436 | 436 | "\n", |
437 | 437 | "__dict__ for instance:\n", |
438 | 438 | "{'radius': 3}\n", |
|
637 | 637 | " margin-top: 10px;\n", |
638 | 638 | " margin-bottom: 10px;\n", |
639 | 639 | "\">\n", |
640 | | - "<strong style=\"color:#006064;\">Exercise 1: Simple Class</strong><br>\n", |
| 640 | + "<strong style=\"color:#006064; display: block; break-after: avoid; page-break-after: avoid;\">Exercise 1: Simple Class</strong>\n", |
641 | 641 | "\n", |
642 | 642 | "<p style=\"color:#006064;\">\n", |
643 | 643 | "Define a class <code>Rectangle</code> with instance attributes <code>width</code> and <code>height</code>. Add the following methods:\n", |
|
705 | 705 | " margin-top: 10px;\n", |
706 | 706 | " margin-bottom: 10px;\n", |
707 | 707 | "\">\n", |
708 | | - "<strong style=\"color:#006064;\">Exercise 2: Class vs. Instance Attributes</strong><br>\n", |
| 708 | + "<strong style=\"color:#006064; display: block; break-after: avoid; page-break-after: avoid;\">Exercise 2: Class vs. Instance Attributes</strong>\n", |
709 | 709 | "\n", |
710 | 710 | "<p style=\"color:#006064;\">\n", |
711 | 711 | "Create a class <code>Counter</code> with a class attribute <code>count</code> initialized to <code>0</code>, and an instance attribute <code>name</code> that is set when a new instance is created. Each time a new instance is created, increase <code>Counter.count</code> by 1 and print the instance's name along with the current value of <code>Counter.count</code>. Create several instances and observe how the class attribute <code>count</code> is shared by all instances, while the instance attribute <code>name</code> is different for each one.\n", |
|
764 | 764 | " margin-top: 10px;\n", |
765 | 765 | " margin-bottom: 10px;\n", |
766 | 766 | "\">\n", |
767 | | - "<strong style=\"color:#006064;\">Exercise 3: Using Inheritance</strong><br>\n", |
| 767 | + "<strong style=\"color:#006064; display: block; break-after: avoid; page-break-after: avoid;\">Exercise 3: Using Inheritance</strong>\n", |
768 | 768 | "\n", |
769 | 769 | "<p style=\"color:#006064;\">\n", |
770 | 770 | "Create a class <code>Shape</code> and a subclass <code>Circle</code> to explore inheritance and the use of <code>super()</code>.\n", |
|
0 commit comments