3

The following code runs ok:

>>> from sklearn import datasets
>>> iris=datasets.load_iris()
>>> type(iris.data)
<type 'numpy.ndarray'>
>>> 

It shows that iris.data is of type numpy.ndarray. So I guess the numpy.ndarray should already be imported. But the following code failed.

>>> help(numpy.ndarray)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'numpy' is not defined

So, how could this numpy.ndarray is not imported but can be used?

smwikipedia
  • 61,609
  • 92
  • 309
  • 482
  • 2
    If all imports in all modules were available in every namespace, that'd be a big ole heap of mess. `sys.modules` holds references to all modules. – Martijn Pieters May 19 '14 at 15:28
  • 1
    It is only defined in the scope of the module that you imported. – anon582847382 May 19 '14 at 15:28
  • 2
    I am disappointed to see a nearly 5k user, one with gold badges, ask such basic questions, btw. You already found the Python glossary, was the [Python reference documentation](https://docs.python.org/2/reference/) a step too far? – Martijn Pieters May 19 '14 at 15:28
  • This is basic scoping logic, you should know this. – anon582847382 May 19 '14 at 15:30
  • 2
    @MartijnPieters Because I just started to learn python for less than 48 hours. – smwikipedia May 19 '14 at 15:30
  • 3
    @MartijnPieters, his points are from other subjects. I'm sure I'd ask seemingly obvious questions if I hopped over to the C# area. – Kirk Strauser May 19 '14 at 15:52
  • @KirkStrauser: but the python documentation covers this quite extensively. The [Python tutorial covers it](https://docs.python.org/3/tutorial/modules.html#more-on-modules) (*Each module has its own private symbol table*), as does the reference documenation (combo of [execution model](https://docs.python.org/3/reference/executionmodel.html), the [import system](https://docs.python.org/3/reference/import.html) and the [`import` statement](https://docs.python.org/3/reference/simple_stmts.html#the-import-statement) documentation). – Martijn Pieters May 19 '14 at 16:04
  • @KirkStrauser: I'd have hoped that someone with this much experience asking questions also knows how to read the documentation or search Stack Overflow for duplicates a little more. – Martijn Pieters May 19 '14 at 16:04
  • 1
    @MartijnPieters Sorry if this bothered you. Maybe the context was not clear. I am working on a very quick experiment with Python. And I have read several crash tutorials on Python. This question just came across my mind and fortunately, it didn't block my experiment. So I just posted it here in the hope of getting a short and quick answer. I guess that's the spirit of online community. I agree the docs are extensive. But unfortunately, I don't have that much time. If there's time, situation will change. – smwikipedia May 20 '14 at 02:50
  • @smwikipedia: Really... I don't think you need to justify extensively. I find it okay to ask even with big reputations and it's very weird to think that you need to give reasons on why and how you ask. The *"I am disappointed to see a nearly 5k user, one with gold badges, ask such basic questions, btw"* is kinda inadequate IMO – tim Jun 11 '14 at 10:52

0 Answers0