From 28dde6c5b0116859ffb2537586b88a79e8efd74b Mon Sep 17 00:00:00 2001 From: Johannes van der Kwast Date: Thu, 3 Jun 2021 09:54:00 +0200 Subject: [PATCH] use of alias added to Chapter 8 --- PythonIntroCh8.ipynb | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/PythonIntroCh8.ipynb b/PythonIntroCh8.ipynb index 585448e..0eb4f94 100644 --- a/PythonIntroCh8.ipynb +++ b/PythonIntroCh8.ipynb @@ -46,7 +46,7 @@ " def printdetails(self):\n", " print(\"This piano is a/an \" + self.height + \" foot\", end=\" \")\n", " print(self.type, \"piano, \" + self.age, \"years old and costing\\\n", - " \" + self.price + \" dollars.\")\n", + " \" + self.price + \" dollars.\")\n", "```\n", "\n", "As you see, a module looks pretty much like your normal Python program.\n", @@ -112,6 +112,19 @@ "\n", "This way, you can remove some crypticness, AND have all of the items from a certain module.\n", "\n", + "A final handy way to import modules is with an alias. Maybe you want to change a name because you've already used the same name for something else in your program, another module you imported uses the same name, or maybe you want to abbreviate a longer name that you use a lot. We can then use the `as` operator. That looks like this:\n", + "\n", + "```Python\n", + "### IMPORT A MODULE WITH AN ALIAS\n", + "# import module\n", + "import moduletest as mt\n", + "\n", + "# use module\n", + "print(mt.age)\n", + "cfcpiano = mt.Piano()\n", + "cfcpiano.printdetails()\n", + "```\n", + "\n", "## 8.4 Conclusion\n", "That's it! A very simple lesson, but now you can organise your programs very neatly. In fact, now it is incredibly easy to make programs that can grow in complexity without ending up with one cryptic file that is full of bugs.\n", "Modules are great for importing code. Next lesson, we learn about file input and output, and the saving of information inside classes, to be retrieved later. Will be great!" @@ -141,7 +154,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.9" + "version": "3.6.5" } }, "nbformat": 4,