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.
Argie90
 
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:
http://docs.python.org/3/reference/simple_stmts.html#import

Specifically:
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).

-Mek
User avatar
Mekire
 
Posts: 820
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

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 irc.freenode.net!
Somelauw
 
Posts: 57
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.
Argie90
 
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
# imported.py
a = 1
b = 2

Code: Select all
# main.py
import imported

# now the a and b from imported.py 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
stranac
 
Posts: 897
Joined: Thu Feb 07, 2013 3:42 pm

Re: Help with NumPy

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

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


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests