johnnyD
johnnyD

Reputation: 59

Python Import directive

  1. What I have found is, that if I'm importing packages and I need shortcut I have to write

    Import numpy.linalg as lg
    

    but i dont know what purpose has to write import directive in these format types,

    import numpy.linalg
    import numpy.numpy.polynomial.polynomial
    

    because those directives imports all the numpy package at the same way as if i would write just only

    import numpy
    

    at the end the syntax for accessing some functions from let's say polynomial is the same

    import numpy
    xx = numpy.polynomial.polynomial.polydiv([2],[6])
    

    for both cases

    import numpy.numpy.polynomial.polynomial
    xx = numpy.polynomial.polynomial.polydiv([2],[6])
    

    so What is the purpose for this ?

  2. Where are stored objects from the NumPy module self? I can find definitions of functions, classes etc. For every single module in NumPy package in their subfolders, but can't find any files for NumPy itself, for example numpy.sin() function.

Upvotes: 1

Views: 760

Answers (1)

user2357112
user2357112

Reputation: 281330

Importing a package, such as numpy, is not guaranteed to import its submodules and subpackages. import numpy happens to load numpy.linalg and numpy.polynomial due to imports performed by the numpy module itself, but this is not a guarantee, and it does not happen for all NumPy submodules. For example,

>>> import numpy
>>> numpy.distutils
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'distutils'
>>> import numpy.distutils
>>> numpy.distutils
<module 'numpy.distutils' from 'C:\Python27\lib\site-packages\numpy\distutils\__init__.pyc'>

As for the source code of objects in the numpy namespace, that varies greatly from object to object, and it can be rather convoluted. For example, numpy.sin is a ufunc compiled from C code generated by numpy/core/code_generators/generate_umath.py, and that code generator uses components from a bunch of other files.

Upvotes: 3

Related Questions