简体   繁体   中英

Working with Python Module/Library (Scipy)

I am new-ish to programming and have a question about the way Python packages and modules are organized and are accessed. I was trying to get a function to calculate permutations and found scipy.special.perm . My first intuition was that scipy was the module, special was the class, and perm was the function. So I tried this (1):

import scipy
scipy.special.perm(5, 4)

But I got the error:

AttributeError: module 'scipy' has no attribute 'special'

This is what worked (2):

import scipy.special
scipy.special.perm(5,4)
  1. Why did (1) fail?
  2. I am trying to understand conceptually how scipy is built/organized. Is scipy a package or a module? After all the error message did refer to it as a module so I am leaning towards the latter.
  3. What is scipy.special then? Is it a module?
  4. What is the recommended way to 'explore' a new module/package like scipy to understand its nature and how it needs to be called so that I now whether to use (1) or (2)?

There are many subpackages inside scipy, most of them are slow to load because they are large Fortran extensions. By default if you import scipy it does not load any submodule, so it's useless in most cases. You always want to import the subpackage you need, for example: import scipy.special or better yet from scipy.special import perm . Here is a list of available submodules in scipy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM