Python Forum
How to find functions or methods in a module?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to find functions or methods in a module?
#1
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?
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
(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
Reply
#5
(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
Reply
#6
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.
Reply
#7
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:~$ 
Reply
#8
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.
Reply
#9
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)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pip lists the module but python does not find it Dato 2 1,271 Apr-13-2023, 06:40 AM
Last Post: Dato
  functions/methods menator01 2 1,627 Oct-02-2021, 07:37 PM
Last Post: menator01
  unable to find module in su mode? korenron 2 1,918 Jan-10-2021, 07:41 PM
Last Post: Gribouillis
  Cannot find module 'torch' ErnestTBass 6 3,618 Oct-13-2020, 05:33 PM
Last Post: ErnestTBass
  How to keep overview about methods in a module? RedGreenBlue 3 1,880 Oct-05-2020, 07:34 AM
Last Post: buran
  module to store functions/variables and how to call them? mstichler 3 2,394 Jun-03-2020, 06:49 PM
Last Post: mstichler
  how to find module in installed packages keuninkske 3 3,195 May-09-2020, 10:21 PM
Last Post: keuninkske
  Improving my understanding of functions and methods menator01 2 2,134 Apr-24-2020, 06:26 AM
Last Post: menator01
  Unable to find an existing module arbiel 2 4,954 Apr-11-2020, 07:12 PM
Last Post: arbiel
  Pycharm Cannot Find Pygame Module Myang123 2 5,981 Aug-07-2019, 01:26 AM
Last Post: Myang123

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020