Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Function Without Calling Module

I am using Canopy with the Jupyter notebook. I was wondering if there was a way to use function from a module without having to call the module. For example if I have

import numpy as np
print np.sin(2)

I would want to be able to just type

print sin(2)

The first thing that comes to mind is to add the numpy functions into whatever function library that Python is using. But I was wondering if this is feasible and, if so, how I could go about doing it. Note that I want to import all functions, not just a select few.

like image 657
T-Ray Avatar asked Apr 18 '17 02:04

T-Ray


People also ask

How do I call a function without defining it in Python?

It is not possible. Python does not allow calling of a function before declaring it like C. This is possible in some languages like JavaScript but not in Python. This means Python does not allows calling before declaring.

Can we call function outside the function in Python?

In Python, any written function can be called by another function. Note that this could be the most elegant way of breaking a problem into chunks of small problems. In this article, we will learn how can we call a defined function from another function with help of multiple examples.

What is difference between module and function?

In programming, function refers to a segment that groups code to perform a specific task. A module is a software component or part of a program that contains one or more routines. That means, functions are groups of code, and modules are groups of classes and functions.

Can a function be a module?

A module is a function or group of similar functions. They are grouped together within a file and contain the code to execute a specific task when called into a larger application.


2 Answers

You can import specific objects from a module. Try:

from numpy import sin

print sin(2)

To import all objects from a module into the global namespace you can use import *.

from numpy import *
print sin(2)

But this is not recommended because you can easily end up with name clashes, e.g. if two modules define a function named sin which version of sin should be called?

>>> import math
>>> import numpy
>>> math.sin
<built-in function sin>
>>> numpy.sin
<ufunc 'sin'>

>>> from math import *
>>> sin
<built-in function sin>
>>> from numpy import *
>>> sin
<ufunc 'sin'>

You can see here that the second import from numpy replaced sin in the global namespace.

For this reason it is best to import the specific objects that you need if there are only a few, otherwise just import the module and use the module name as a prefix (as per your first example). In my example if you wanted to use both math.sin and nump.sin you would either need to import the modules only and prefix using the module name, or import the functions and rename them like this:

from numpy import sin as np_sin
from math import sin
like image 51
mhawke Avatar answered Sep 21 '22 08:09

mhawke


from numpy import sin
print sin(2)

https://docs.python.org/2/tutorial/modules.html read this in details

like image 38
Kallz Avatar answered Sep 19 '22 08:09

Kallz