How to make a python module or function and use it while writing other programs?

Question:

There where many instances where I have to write large line of code over and over again in multiple programs. So I was wondering if I could write just one program, save it and then call it in different programs like a function or a module.

An elementary example: I write a program to check if a number is palindromic or not. Then I want to write a program to check if a number is palindromic and a prime, could I just call the first program and do the rest of the code to check if it is prime or not?

Asked By: steve

||

Answers:

You should check out the python documentation. If you still have questions then come back.

https://docs.python.org/2/tutorial/modules.html

Answered By: bravosierra99

Yes. Let’s say you’re writing code in a file called palindrome.py. Then in another file, or in the python shell you want to use that code. You would type

import palindrome

either at the top of the file or just into the shell as a command. Then you can access functions you’ve written in palindrome.py with statements like

palindrome.is_palindrome('abba')

It’s important to note that to do this propery, the code in palindrome.py must be in functions. If you’re not sure how to do that, I recommend the official Python tutorial: https://docs.python.org/3/tutorial/index.html

Answered By: Patrick Haugh

it’s easy to write a function in python.

first define a function is_palindromic,

def is_palindromic(num):
    #some code here
    return True

then define a function is_prime,

def is_prime(num):
    #some code here
    return True

at last, suppose you have a number 123321, you can call above function,

num = 123321
if is_palindromic(num):
    if is_prime(num):
        print 'this number is a prime!'

ps, maybe you could try to use some editors like vscode or sublime.

Answered By: WanderInCode

It is about writing reusable code. I can suggest you to write reusable code in specific function in separate python file and import that file and function too.
For example you need function called sum in other function called “bodmas” then write function called sum in one python file say suppose “allimports.py”:

    def sum(a,b):
      return a+b

Now suppose your “bodmas” named function is some other python file then just import all the required functions and use is normally by calling it.

    from allimports import sum
    def bodmas:
       print(sum(1,1))

One important thing is be specific while import your module as it will effect performance of your code when length of your code is long.
Suppose you want to use all functions then you can use two option like :

    import allimports
    print(allimports.sum(1,1))

other option is

    from allimports import *
    print(sum(1,1))

and for specific imports is as follows:

    from allimports import sum
    print(sum(1,1))
Answered By: Raj Damani
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.