#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Question Why is using "from pylab import *" not bad practice?


    Hello,

    I am a novice user of matplotlib/numpy/scipy. I was looking for tutorials about matplotlib, and with that I often encounter tutorials which teach to use the following:
    Code:
    from pylab import *
    This is even taught in a tutorial (http://www.loria.fr/~rougier/teaching/matplotlib/) which seems to be highly respected, since it is linked to on the matplotlib frontpage (matplotlib.org/).

    I am so surprised by this though, since I've always learned that using "from module import *" is bad practice because you don't know what you are importing, which might cause imported classes to interfere with existing namespaces in your scripts.

    So I wonder, is this really the correct way of using matplotlib? And more importantly; what am I actually importing with this command?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    If you're just using the interpreter and can tolerate name space conflicts,

    from module import *

    makes good sense. You won't have to type as much.

    When writing a script you'll make it easier on whomever reads that script if you explicitly name everything you import. They'll know where to look for documentation.

    import numpy
    asarray = numpy.asarray

    from math import cos

    import tkinter
    root = tkinter.Tk()
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0
    Alright, thanks. But I would like to use matplotlib in a program and therefore want to import everything explicitly.

    What is the verbose equivalent to "from pylab import *"?

    Something like this?:
    Code:
    import numpy as np
    import scipy
    from matplotlib import *
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    http://www.python.org/doc/essays/styleguide.html
    Follow the 2 Guido Van Rossum style guides. You'll have the support of big guns in any style arguments that follow.

    Read python code from the python module library.

    Start here?
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    610
    Rep Power
    65
    You should use
    import matplotlib.pylab as pylab
    to give it a namespace

    Example:
    Code:
    import matplotlib.pylab as pylab
    
    #x_list = [1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6]
    # easier ...
    x_list = pylab.arange(1, 6.5, 0.5)
    y_list = [x*x for x in x_list]
    # default is a blue line
    # 'ro' uses red dots as markers
    pylab.plot(x_list, y_list, 'ro')
    pylab.ylabel('y = x * x')
    pylab.xlabel('x')
    pylab.grid(True)
    pylab.show()
    Without the namespace you have all these functions like
    plot(), grid(), ylabel(), show() etc. that makes it confusing for anybody that reads your code. It will give you away as lazy and inconsiderate.
    Last edited by Dietrich; December 1st, 2012 at 01:33 PM.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

IMN logo majestic logo threadwatch logo seochat tools logo