Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Basic] Python3 mental model
#1
Python 3 mental model

I am new to Python and have no computer science background. Even though there are lots of excellent resources and guides to learn Python, I have not found a good resource to help build a mental model of how Python works, especially for people with no computer science back ground.

Here is my attempt at building one such mental model of Python 3, with the help of a story. This was inspired by the talk of Brandon Rhodes. I have also prepared some example code files as Jupyter notebook files to test the model, which I am gradually building at my Github page. I thought this might be useful to other beginner learners as well. I am also hoping to get some feedback, to correct and improve my own mental model.


STORY
Imagine Python 3 as a magical realm. There are few things in this realm.
There is a Genie, a big recycle bin and lots of sleeping princesses.
Thank you,
Satya.

Genie:
This is the Python language it self, to whom you need to make requests with special incantations(syntax). The genie makes any “object” you want, as long as you use the special incantations. If you ask for another object, the genie does not store the object it just created but just tosses it in the recycle bin (most of the time) and makes a new object for you. If your incantation is incorrect the genie complains with error message.

Objects:
Well, the genie can’t give you any object you want (that was a lie, unfortunately) but some objects which have specific properties. We will discuss 4 of these objects here. These are numbers, words, trains and magic spell boxes. Python can make other objects called locked_trains(tuples), named_trains(dictionaries), bookmarks(generators) etc.

Numbers(integers):
Any number you ask for. Genie builds that number, puts a lock around it so that it can’t be changed any more and gives it to you. Python is good with numbers, so it does the calculations too, if you ask for it.

Words(strings):
Anything which you put in between “ and “ or ‘ and ‘. it can even be an empty word like “” or ‘’, or an empty space “ “ or ‘ ‘. Python again puts lock around that word, so it can’t be changed after it is built.

Trains(lists):
These are trains which have numbered carriages from 0 onwards. Each carriage can hold objects such as a number or word or even a train in it. (Yes a train in a carriage, this is magical realm after all). You can add or remove carriages and change the stuff you put in each carriage. There are no locks here. Genie can make an empty train with no carriages, but if you want to attach a carriage you need to put some object in that carriage.

A magic spell box(functions):
This is a box with a name on it and some instructions of magic in it. You can have some sleeping princess in this box as well. That magic spell will come to life when you call it, with () next to the spell name. When you do that, it actually creates a sub-realm, where all the magic happens(but that is a separate story). At the end of the magic usually an object is created(number or word) or modified(if it is train etc). It returns the object it created back to the main realm as long as you put the right incantation to return it.

Sleeping princess(variables):
If you don’t want the genie to recycle the objects, one of the sleeping princess needs to wake up and hold it for you. They wont wake up unless you give them a name. They won’t stay awake unless you give them an object to hold. You and python recognise something in your code as a princess because their name starts with a letter without “ or ‘ around it (or with an _ ). You can make the princess sleep with del incantation. Then the princess forgets the name, leaves the object and goes back to sleep.

Please visit my Github page for several example code files to test this model.
Thank you,
Satya.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020