Saturday, 5 December 2015

Python: import functions from module directly

By using from, import keywords we can import functions from modules directly.

arithmetic.py
print("Simple api to perform arithmetic operations")
def sum(a, b):
  return a+b

def subtract(a, b):
  return a-b
  
def mul(a,b):
  return a*b
  
def div(a, b):
  return a/b

>>> from arithmetic import sum, subtract, mul, div
>>> 
>>> sum(10, 20)
30
>>> subtract(20, 30)
-10


Use the statement ‘from fibo import *’  to import all the functions. This imports all names except those beginning with an underscore (_).



Previous                                                 Next                                                 Home

No comments:

Post a Comment