The Importing Hack
Tags: programming, beginners, python, productivity
Published: 2022-05-20 19:06:43

When I started programming, I was fine with having the longest lines of code. Well, as long as it works, right?

After some time, I discovered that no one could decipher my code... Much like my handwriting.

And how would I grow or even get hired if no one else could read my code?

Like every other programmer, I had to find the  solution. And yes, I got it.

The "importing hack" solution.

Importing and exporting modules are already familiar concepts. I would like to discuss how to import and export our function.

Imagine turning a code that is this lengthy 

```
def chatWithFriend():
  print("what is your name")
  friend = input()
  print(f' good day {friend}')
  print(f' how are you, {friend}')
  resp1 = input()
  print(f'what is your favorite food,{friend}')
  print('My favourite food is fried rice')
  print('would like to eat my favorite food')
  resp2 = input()
  print(f'what school do you attend, {friend}')
  print('I attend the university of Nsukka, Nigeria')
  resp3 = input()

chatWithFriend()
```

...Into this. And yes, it works.

```
#message.py
from greet import *

def chatWithFriend():
  print("what is your name")
  friend = input()
  greetFriend(friend)
  resp1 = input()
  foodFriend(friend)
  resp2 = input()
  schoolFriend(friend)
  resp3 = input()
```

The trick is this: not using one big function. It would always get very messy. So, why not divide the functions into smaller functions?

Make a new file, input this functions and import them into your existing one.

Here are two ways to import these functions 

1. Importing only one function 
   This is used when only one function has to be imported.
```
#greet.py
def greetFriend(friend):
  print(f' good day {friend}')
  print(f' how are you, {friend}')
```
```
#message.py
from greet import greetFriend

def chatWithFriend():
  print("what is your name")
  friend = input()
  greetFriend(friend)
```
2. Importing all the functions
   This will import all of the file's functions into our main file.

```
#greet.py
def greetFriend(friend):
  print(f' good day {friend}')
  print(f' how are you, {friend}')

def foodFriend(friend):
  print(f'what is your favorite food,{friend}')
  print('My favourite food is fried rice')
  print('would like to eat my favorite food')

def schoolFriend(friend):
  print(f'what school do you attend, {friend})
  print('I attend the university of Nsukka, Nigeria')
```
```
#message.py
from greet import *

def chatWithFriend():
  print("what is your name")
  friend = input()
  greetFriend(friend)
  resp1 = input()
  foodFriend(friend)
  resp2 = input()
  schoolFriend(friend)
  resp3 = input()
  
```
### Bonus Tip

* Importing only a number of functions
   A specific variable named __ all __ can be used in modules to limit which variables are imported.

```
#greet.py

__all__ = ['greetFriend', 'foodFriend']

def greetFriend(friend):
  print(f'Good day {friend}')
  print(f'How are you, {friend}')

def foodFriend(friend):
  print(f'What is your favorite food,{friend}')
  print('My favourite food is fried rice')
  print('Would like to eat my favorite food')

def schoolFriend(friend):
  print(f'What school do you attend, {friend}')
  print('I attend the university of Nsukka, Nigeria')
```
```
#message.py
from greet import *

def chatWithFriend():
  print("what is your name")
  friend = input()
  greetFriend(friend)
  resp1 = input()
  foodFriend(friend)
  resp2 = input()
  
```


We all share the programmer's mentality: "If it works, don't change it." I had it too, who didn't? Absolutely no one. 

How did I overcome it? I had to to speak to myself. I didn't want to remain a beginner indefinitely, and I could only progress if my code was readable. It was difficult, but I was able to convince myself.

If I could do that then, you can. Why not use the imports and exports function?