DEV Community

Cover image for The Importing Hack
Mandyiee
Mandyiee

Posted on

The Importing Hack

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()
Enter fullscreen mode Exit fullscreen mode

...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()
Enter fullscreen mode Exit fullscreen mode

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}')
Enter fullscreen mode Exit fullscreen mode
#message.py
from greet import greetFriend

def chatWithFriend():
  print("what is your name")
  friend = input()
  greetFriend(friend)
Enter fullscreen mode Exit fullscreen mode
  1. 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')
Enter fullscreen mode Exit fullscreen mode
#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()

Enter fullscreen mode Exit fullscreen mode

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')
Enter fullscreen mode Exit fullscreen mode
#message.py
from greet import *

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

Enter fullscreen mode Exit fullscreen mode

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?

Top comments (0)