Help with NumPy

This is the place for queries that don't fit in any of the other categories.

Help with NumPy

Postby Argie90 » Thu Jan 02, 2014 11:55 am

Hi, i have a problem, in the command:
Code: Select all
from numpy import *

I can't find what the asterisk mean. I have search all the google and i found nothing, please if somone knows help.
Last edited by Mekire on Thu Jan 02, 2014 12:11 pm, edited 1 time in total.
Reason: First post lock. Slight edit.
Posts: 3
Joined: Thu Jan 02, 2014 11:41 am

Re: Help with NumPy

Postby Mekire » Thu Jan 02, 2014 12:15 pm

Firstly, never use star imports. They are bad. Trust us. Leave it at that.

An explanation of what they are can be found here:

If the list of identifiers is replaced by a star ('*'), all public names defined in the module are bound in the local namespace for the scope where the import statement occurs.

The public names defined by a module are determined by checking the module’s namespace for a variable named __all__; if defined, it must be a sequence of strings which are names defined or imported by that module. The names given in __all__ are all considered public and are required to exist. If __all__ is not defined, the set of public names includes all names found in the module’s namespace which do not begin with an underscore character ('_'). __all__ should contain the entire public API. It is intended to avoid accidentally exporting items that are not part of the API (such as library modules which were imported and used within the module).

New Users, Read This
  • Use code tags when posting code.
  • Include any errors with your post (in code tags).
  • Describe your problem; not your chosen solution.
  • Make examples the minimum length to demonstrate your issue.
User avatar
Posts: 1626
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

Re: Help with NumPy

Postby Somelauw » Thu Jan 02, 2014 7:16 pm

In other words, it's a shortcut for:
Code: Select all
from numpy import array
from numpy import zeros
from numpy import random

As Mekire said, it's not a safe way to import stuff.

Better is to use:
Code: Select all
import numpy as np
arr = np.array([1, 3, 7])
Join the #python-forum IRC channel on!
Posts: 82
Joined: Tue Feb 12, 2013 8:30 pm

Re: Help with NumPy

Postby Argie90 » Fri Jan 03, 2014 12:22 pm

Thank you guys, i have two more questions what exactly is a module and what is namespace in Python, with an example if you like. Thanks.
Posts: 3
Joined: Thu Jan 02, 2014 11:41 am

Re: Help with NumPy

Postby stranac » Fri Jan 03, 2014 12:42 pm

From the python tutorial(Modules):
A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended.

Namespaces are...thingies...that contain some names.
They're useful in a few ways, like knowing where stuff is coming from, and allowing you to have same names in multiple places.

Might be easier to explain through an example:
Code: Select all
a = 1
b = 2

Code: Select all
import imported

# now the a and b from are in the imported namespace
# and must be accessed by imported.a and imported.b
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
Posts: 1692
Joined: Thu Feb 07, 2013 3:42 pm

Re: Help with NumPy

Postby Argie90 » Wed Jan 08, 2014 5:39 pm

Thank you guys!
Posts: 3
Joined: Thu Jan 02, 2014 11:41 am

Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot] and 5 guests