Python Forum
How to find functions or methods in a module? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to find functions or methods in a module? (/thread-17603.html)



How to find functions or methods in a module? - deepakdeshp - Apr-17-2019

Hello,
If I import a module , how do I find all the functions in the module?
In the following code math will have many more functions like sqrt. How do I find them without resorting to Google?
import math
math.sqrt(16)
Another question is how do I find a suitable module to perform a task?


RE: How to find functions or methods in a module? - ichabod801 - Apr-17-2019

dir(math) will show you the contents of the module, although constants and classes as well as function. You can type help(math.object) to get the help text for a particular object in the math module.


RE: How to find functions or methods in a module? - perfringo - Apr-17-2019

(Apr-17-2019, 04:23 PM)deepakdeshp Wrote: In the following code math will have many more functions like sqrt. How do I find them without resorting to Google?

In interactive interpreter one can do this:

>>> import math
>>> math.        # two times TAB
math.acos(       math.erf(        math.inf         math.pi
math.acosh(      math.erfc(       math.isclose(    math.pow(
math.asin(       math.exp(        math.isfinite(   math.radians(
math.asinh(      math.expm1(      math.isinf(      math.remainder(
math.atan(       math.fabs(       math.isnan(      math.sin(
math.atan2(      math.factorial(  math.ldexp(      math.sinh(
math.atanh(      math.floor(      math.lgamma(     math.sqrt(
math.ceil(       math.fmod(       math.log(        math.tan(
math.copysign(   math.frexp(      math.log10(      math.tanh(
math.cos(        math.fsum(       math.log1p(      math.tau
math.cosh(       math.gamma(      math.log2(       math.trunc(
math.degrees(    math.gcd(        math.modf(       
math.e           math.hypot(      math.nan 
>>> help(math.log)     # help for specific function
Help on built-in function log in module math:

log(...)
    log(x, [base=math.e])
    Return the logarithm of x to the given base.
    
    If the base not specified, returns the natural logarithm (base e) of x.
(END)



RE: How to find functions or methods in a module? - deepakdeshp - Apr-18-2019

(Apr-17-2019, 06:43 PM)perfringo Wrote:
(Apr-17-2019, 04:23 PM)deepakdeshp Wrote: In the following code math will have many more functions like sqrt. How do I find them without resorting to Google?

In interactive interpreter one can do this:

>>> import math
>>> math.        # two times TAB
math.acos(       math.erf(        math.inf         math.pi
math.acosh(      math.erfc(       math.isclose(    math.pow(
math.asin(       math.exp(        math.isfinite(   math.radians(
math.asinh(      math.expm1(      math.isinf(      math.remainder(
math.atan(       math.fabs(       math.isnan(      math.sin(
math.atan2(      math.factorial(  math.ldexp(      math.sinh(
math.atanh(      math.floor(      math.lgamma(     math.sqrt(
math.ceil(       math.fmod(       math.log(        math.tan(
math.copysign(   math.frexp(      math.log10(      math.tanh(
math.cos(        math.fsum(       math.log1p(      math.tau
math.cosh(       math.gamma(      math.log2(       math.trunc(
math.degrees(    math.gcd(        math.modf(       
math.e           math.hypot(      math.nan 
>>> help(math.log)     # help for specific function
Help on built-in function log in module math:

log(...)
    log(x, [base=math.e])
    Return the logarithm of x to the given base.
    
    If the base not specified, returns the natural logarithm (base e) of x.
(END)

Thank you. The double tab doesnt work for Python2.7 but does work for Python3.7 on Linux Mint


RE: How to find functions or methods in a module? - snippsat - Apr-18-2019

(Apr-18-2019, 06:09 PM)deepakdeshp Wrote: The double tab doesnt work for Python2.7 but does work for Python3.7 on Linux Mint
Or better use ptpython or ipython.
I like ptpython(review) as there is no need to push Tab to see methods.
Also on Windows use cmder then get nice color when use ptpython/ipython.
cmd/powershell is just bad Sick


RE: How to find functions or methods in a module? - deepakdeshp - Apr-19-2019

Thank you.
Which Python ide is good for a beginner? I am using Linux Mint 19.1. I have installed Spyder,THonny and Pycharm. I can install others too , if required.


RE: How to find functions or methods in a module? - snippsat - Apr-19-2019

Can look at VS Code from start
The IntelliSense in editor is very good,eg for your your first question to find function/methods it will do it with just mouse over code.
I also use Mint 19 when on Linux,it comes with Python 3.6.5 look at pyenv Simple Python Version Management
Then is easy to install and switch to whatever version want to use.
tom@tom-VirtualBox:~$ python -V
Python 3.7.2

# Can eg switch back to default Mint setup 
tom@tom-VirtualBox:~$ pyenv local system
tom@tom-VirtualBox:~$ python -V
Python 2.7.15rc1
tom@tom-VirtualBox:~$ python3 -V
Python 3.6.5

# No back again i want to use 3.7
tom@tom-VirtualBox:~$ pyenv local 3.7.3
tom@tom-VirtualBox:~$ python -V
Python 3.7.3
tom@tom-VirtualBox:~$ pip -V
pip 19.0.3 from /home/tom/.pyenv/versions/3.7.3/lib/python3.7/site-packages/pip (python 3.7)
tom@tom-VirtualBox:~$ 



RE: How to find functions or methods in a module? - deepakdeshp - May-23-2019

Thank you.Its a good tip. dir () returns list of the attributes. Is there any way to get help on a particular attribute , preferably with a usage example of the attribute? help()does give the help on the attribute but there is no example.


RE: How to find functions or methods in a module? - DeaD_EyE - May-23-2019

You can't do this for types like str, int, float etc.
You can make a doctstring for functions, methods and properties.

class Foo:
    """
    This is a dummy class. Nothing special going on here.
    """
    def __init__(self, name):
        """
        Method to initialize the class. Only the name is needed.
        """
        self.name = name
    @property
    def hello_property(self):
        """
        This property returns a greeting
        """
        return f'Hello {self.name}'

    def hello_method(self):
        """
        Hello Method returns a greeting
        """

help(Foo)
Output:
Help on class Foo in module __main__: class Foo(builtins.object) | Foo(name) | | Methods defined here: | | __init__(self, name) | Initialize self. See help(type(self)) for accurate signature. | | ---------------------------------------------------------------------- | Data descriptors defined here: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined) | | hello | This property returns a greeting (END)
If you use for example enum, you can document your code with names.

import enum


class Color(enum.IntEnum):
    red = 0
    green = 1
    blue = 2

help(Color)
Output:
Help on class Color in module __main__: class Color(enum.IntEnum) | Color(value, names=None, *, module=None, qualname=None, type=None, start=1) | | An enumeration. | | Method resolution order: | Color | enum.IntEnum | builtins.int | enum.Enum | builtins.object | | Data and other attributes defined here: | | blue = <Color.blue: 2> | | green = <Color.green: 1> | | red = <Color.red: 0> | | ---------------------------------------------------------------------- | Data descriptors inherited from enum.Enum: | | name | The name of the Enum member. | | value | The value of the Enum member. | | ---------------------------------------------------------------------- | Data descriptors inherited from enum.EnumMeta: | | __members__ | Returns a mapping of member name->value. | | This mapping lists all enum members, including aliases. Note that this | is a read-only view of the internal mapping.
If you go further, you could use type hinting, which is good for IDEs.

def calc_delta_range(bandwidth: int) -> float:
    """
    Calculate the delta range of a K-MD2
    
    bandwidth := frequency in MHz
    returns := delta range in meter
    """
    return 150e6 * (257 / (256 * bandwidth * 1e6))


help(calc_delta_range)
Output:
Help on function calc_delta_range in module __main__: calc_delta_range(bandwidth: int) -> float Calculate the delta range of a K-MD2 bandwidth := frequency in MHz returns := delta range in meter (END)