DEV Community

Cover image for The Start of Something Beautiful
Snehangsu De
Snehangsu De

Posted on • Updated on

The Start of Something Beautiful

"For millions of years mankind lived just like the animals.
Then something happened which unleashed the power of our imagination,
We learned to talk."

Quoting from the lines of Pink Floyd, it's simple to understand how communication has shaped our society, likely the single most important thing that has made civilizations stand. While we have created types of communication for us humans, since the age of machines we have learned to communicate with machines too, and surprisingly it spans back to the 40s.

I wanted to start with the history as to where do programming languages come from, as they help us understand why we are working with them. Plus it narrates a nice story on how far we have come. As we begin, today we will be discussing Import Statements. However, here's a quick look at what the first high-level language looked like.

'Hello World' in Plankalkül

Traumatizing, right? I agree we have come a long way from the above image to print('Hello, world!').

Let's begin!

Section 1: Imports in Python 🐍

To help us understand what and how imports work, let's assume we have a module (basically a sheet of code) named dinosaur.py that has

type = 20 [A variable]

def roar(name):
... return f'{name} roars'

We can import this module in two different ways :

Import statement 1:

import dinosaur

Import statement 2:

from dinosaur import roar

Here are few general pointers for import -

  • Imports are statements and not functions
  • Importing dinosaur.py creates a variable name dinosaur and also creates another variable type which is inside the dinosaur.py module
  • Importing creates a module object (object can be defined as a blueprint) and sets a variable (the name of the module) to point to that module object. [Defined in the 3rd point]

Running Import statement 1 executes the module line by line. So, when I call the statement :

  1. It defines the global variable type as an integer.
  2. Defines the global variable roar as a function that takes one argument.
  3. Then import defines a variable dinosaur, through whose attributes (dinosaur.type and dinosaur.roar) we can get to our variable and function.

Running Import statement 2 executes the module but only by the respective variable roar. So, when I call the statement :

  1. It defines only the variable roar to be used inside the program.
  2. It would not define a variable under the module name, so we would not have a variable named dinosaur anymore.

There is one other rare way to import which should be avoided at all costs.

from dinosaur import * [This causes to replace all variables in your local namespace]

A namespace is the current *.py file we would be working on, or importing the statements to.

Here's a picture to understand better how variables work inside namespaces when imported.

Imports w.r.t Namespace

Hope this has helped you understand how imports work. As this being my first article, if you have any interesting suggestions, feel free to connect with me on Twitter. I also have a newsletter, which I send out every week Thursday. You can subscribe it here.

I hope this message finds you in good health, see you next week!

Top comments (0)