0

I've tried googling (to little avail) to more clearly understand what different meaning period . has during an import statement, vs. once a module has already been imported.

For example, these all work:

import numpy
X = numpy.random.standard_normal

from numpy.random import standard_normal

import numpy.random

but this doesn't work:

import numpy.random.standard_normal

I'm a bit confused as to why this is. Why is there a difference in what the period . does when accessing a module before vs. after an import?

1 Answer 1

2

It's because standard_normal is a method

<built-in method standard_normal of numpy.random.mtrand.RandomState object at 0x0000029D722FBD40>

whenever you do from numpy.random import standard_normal you are importing the method

and i don't think you can do this import numpy.random.standard_normal cause standard_normal again is a method, this would be possible if standard_normal would be some module.

Take a look at this, you when I typed dir(standard_normal) I get the output of those things which are attributes and when I typed standard_normal it says <built-in method standard_normal of numpy.random.mtrand.RandomState object at 0x000002509D504740> cause it simply says it is a method

sample

Now when I did this import numpy.random.standard_normal , you are expecting to import the method right? But what it really does is trying to import a module, Well... there is no such thing as standard_normal module or standard_normal.py file.

Take a look at this again. I imported the random module and I used the . operator to access the standard_normal function. You can see the sense of it right. Cause on the random.py module it has there a standard_normal function or method.

sampe

Sorry I had to use the CMD.

Sign up to request clarification or add additional context in comments.

6 Comments

I observed that, but I was curious if there's some underlying issue as to why the import X.Y statement only works for modules, rather than functions.
@user49404 why do you think it's an issue? It's how the language works... You import modules. If you just want to have the name of a specific function from another module you can do from module import function
@Tomerikoo It's not an issue, I was just curious why the language was designed that way.
@user49404 , why may I ask are there other programming languages that you have experienced that applies on what you these concept like import random.random.standard_normal actually imports the standard_normal function. Cause I think that's not how it works with python.
@user49404 seems logic and consistent... Pretty straightforward also import (the module) module and from (the module) module import (the function) function (parenthesis added to illustrate how to read the actual code). Anyway this kind of questions can't be answered here... We are just experienced users, it is a question for the designers of the language
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.