4

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?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
steve
  • 125
  • 12
  • 1
    Rewrite your programs as functions and put them in a module. – polku Oct 11 '16 at 13:36
  • 2
    Start with some Googling or searching on this site. This is one of the top few results of a Google search - [How to write a python module](https://stackoverflow.com/questions/15746675/how-to-write-a-python-module) – thephez Oct 11 '16 at 13:38
  • 1
    See [How much research effort is expected of Stack Overflow users?](http://meta.stackoverflow.com/q/261592/7432) – Bryan Oakley Oct 11 '16 at 14:06

3 Answers3

3

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))
Raj Damani
  • 782
  • 1
  • 6
  • 19
2

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

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
0

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.