Modules and Methods 模块和方法

1.1.3: Modules and Methods 模块和方法

让我们谈谈模块。
Let’s talk a little bit about modules.
Python模块是代码库,您可以使用import语句导入Python模块。
Python modules are libraries of code and you can import Python modules using the import statements.
让我们从一个简单的案例开始。
Let’s start with a simple case.
我们将通过说“导入数学”来导入数学模块。
We’re going to import the math module by saying “import math”.
该模块具有多个功能。
The module comes with several functions.
我们将演示其中的几个。
We’re just going to demonstrate a couple of them.
例如,如果我想使用pi的值,我会键入math.pi,Python会告诉我pi的值,3.14,依此类推。
For example, if I wanted to use the value of pi, I would type math.pi and Python would tell me the value of pi, 3.14, and so on.
数学模块还附带了几个函数或方法。
The math module also comes with several functions, or methods.
让我们试试平方根函数。
Let’s try out the square root function.
要使用它,我键入math.sqrt,输入参数是我希望平方根取的数字。
To use that, I type math.sqrt and the input argument is the number that I want the square root to be taken of.
在本例中,我要求Python返回10的平方根值。
So in this case, I’ve asked Python to return the value of square root of 10.
让我们做一些更复杂的事情。
Let’s do something a little more sophisticated.
如果我想找出sin pi除以2的值呢?
What if I wanted to find out the value of sin pi over 2?
让我们首先提取pi的值,我们知道它是math.pi。
Let’s first extract the value of pi, which we know is math.pi.
我们可以把这个数除以2。
We can then take this number and divide that by 2.
这是π除以2。
So this is pi over 2.
为了得到这个数字的sin,我们说math.sin并使用math.pi除以2作为sin函数的输入。
To take the sin of this number, we say math.sin and use math.pi over 2 as an input to the sin function.
正如所料,Python告诉我们π除以2的sin正好是1。
And as expected, Python tells us the sin of pi over 2 is exactly 1.
有时,我们不想使用整个模块。
Sometimes, we don’t want to use the entire module.
也许我们只想从该模块中选择一个函数。
Perhaps we just want to choose one function from that module.
让我们考虑一种情况,在这种情况下,我只需要能够在我的程序中使用pi的值。
Let’s think about a situation where I just need to be able to have the value of pi available to me in my program.
所以我不需要数学库中的任何东西,只需要π的值。
So I didn’t need anything else from the math library, just the value of pi.
要做到这一点,我可以做以下工作——我可以从数学中区分Python,导入pi。
To do that, I can do the following– I can tell Python from math, import pi.
在本例中,Python只从该模块导入了pi的值,而没有导入其他内容。
In this case, Python has imported just the value of pi from that module and nothing else.
让我们稍微谈谈名称空间。
Let’s talk a little bit about namespaces.
什么是名称空间?
What is a namespace?
一个共享的容器名称通常是一个共享的容器名称。
Well namespace is a container of names shared by objects that typically go together.
其目的是防止命名冲突。
And its intention is to prevent naming conflicts.
也许理解名称空间的最好方法是通过一个例子。
Perhaps the best way to understand namespace is through an example.
那么让我们做下面的事情。
So let’s do the following.
让我们先导入数学模块。
Let’s import the math module first.
然后我们将numpy模块作为np导入。
We’re then going to import the numpy module as np.
现在,数学模块有一个平方根方法sqrt,但是numpy也有一个平方根方法sqrt。
Now, the math module has a square root method, sqrt,but numpy also has a square root method, sqrt.
这两种功能之间有什么区别?
What is the difference between these two functions?
好吧,让我们来举个例子。
Well, let’s try an example.
如果我键入math.sqrt,我可以让Python计算2的平方根的值。
If I type math.sqrt, I can ask Python to calculat the value of the square root of 2.
我可以使用numpy模块中的平方根函数做同样的事情。
I can do the same exact thing using the square root function from the numpy module.
到目前为止,这两个函数似乎是相同的,但实际上这两个函数是完全不同的,它们存在于不同的名称空间中。
So far, it appears that these two functions are identical,but actually these two functions are quite separate and they exist in different namespaces.
事实证明,numpy平方根函数可以做数学平方根函数不知道如何做的事情。
It turns out that the numpy square root function can do things that the math square root function doesn’t know how to do.
这里有一个简单的例子。
Here’s a simple example.
如果我不仅要取一个数的平方根,还要取几个数的平方根,比如说,2,3,4?
What if I wanted to take the square root of not just one number,but several numbers, say, 2, 3, and 4?
我可以同时为他们所有人做这件事。
I can do this simultaneously for all of them.
然后我们来讨论import语句。
Let’s then talk about the import statement.
运行Python导入语句时会发生什么?
What exactly happens when you run the Python import statement?
发生了三件事。
Three things happen.
首先,Python为新模块中定义的所有对象创建一个新的名称空间。
The first thing that happens is Python creates a new namespace for all the objects which are defined in the new module.
所以在抽象意义上,这是我们的新名称空间。
So in abstract sense, this is our new namespace.
这是第一步。
That’s the first step.
Python执行的第二步是执行模块的代码,并在新创建的名称空间中运行它。
The second step that Python does is it executes the code of the module and it runs it within this newly created namespace.
发生的第三件事是Python创建了一个名称——比方说np代表numpy——这个名称引用了这个新的名称空间对象。
The third thing that happens is Python creates a name– let’s say np for numpy– and this name references this new namespace object.
因此,当您调用np.sqrt函数时,Python正在numpy名称空间中使用sqrt函数。
So when you call np.sqrt function, Python is using the sqrt function within the numpy namespace.
Math有自己的名称空间和自己的平方根函数。
Math has its own namespace with its own square root function.
因此,当您键入math.sqrt时,Python正在math命名空间中调用此sqrt函数。
So when you type math.sqrt, Python is calling this sqrt function within the math namespace.
如果有人递给你一个Python对象,而你不知道该对象的类型,该怎么办?
What if somebody hands you a Python object and you don’t know the type of that object?
让我们看看你能做些什么。
Let’s see what you can do.
让我们定义一个Python字符串。
Let’s define a Python string.
我们要定义一个名字,我们要叫这个名字艾米。
We’re just going to define a name, and we’re going to call that name Amy.
如果您想知道这个对象的类型,可以使用type函数,Python会告诉您名称是字符串。
If you wanted to know what is the type of this object,you can use the type function, and Python will tell you the name is a string.
现在您知道对象是一个字符串,
Now that you know that the object is a string,
我们可以找到您可以使用的其他方法。
we can find out what other methods that are available to you.
你可以用两种不同的方式来做。
You can do this in two different ways.
我们可以使用todir,dir函数,得到一个目录的方法。
We can use to dir, dir function, to get a directory of the methods.
因此,我可以键入dir name,Python将为我提供一长串可用于字符串对象的方法。
So I can type dir name, and Python will give me a long list of methods that are available to string objects.
另一种方法是使用对象类型,而不是使用对象的实际名称。
An alternative way to do this is instead of using the actual name of the object,I can use the object type.
在本例中,我知道name是一个字符串,所以我可以只键入str而不键入name,Python将给出相同的方法列表。
In this case, I know that name was a string, so instead of typing name,I can just type str and Python will give me the same exact list of methods.
因此,如果我想了解更多关于特定方法的知识,比如说,字符串的上限方法,我应该怎么做?
So if I wanted to learn more about a specific method, say, the upper method of strings, what should I do?
那么,第一个选择是如下。
Well, the first option is the following.
我可以通过键入“help”向Python寻求帮助,Python将给我一个非常简短的函数描述。
I can just ask Python for help by typing “help” and Python will give me a very brief description of the function.
但是请注意以下几点——当我请求帮助时,我需要确保我没有实际运行该方法。
But notice the following– when I am asking for help,I need to make sure that I don’t actually run that method.
那么这里发生了什么?
So what’s happening here?
upper是绑定到Name对象的函数或方法。
Name.upper is a function, or a method, that’s bound to a name object.
如果我在结尾加上括号,我实际上是在调用该方法。
If I add parentheses to the end, I’m actually calling that method.
我要求Python做点什么。
I’m asking Python to do something.
在这种情况下,Python将名称“Amy”(在本例中)转换为全大写。
In that case, Python is turning the name “Amy,” in this case,into all uppercase.
因此,如果我现在请求帮助,如果我键入“help”name.upper,在upper后面加上括号,我实际上是在请求艾米的帮助。
So if I’m now asking for help, if I type “help” name.upper and I have the parentheses after upper, I’m effectively asking help on Amy.
所以在本例中,Python告诉我它没有可供Amy使用的文档。
So in this case, Python tells me it doesn’t have documentation available for Amy.
因此,当我使用帮助函数时,我需要确保我没有实际运行我想获得帮助的方法。
So when I’m using the help function, I need to make sure that I don’t actually run the method I’m interested in getting help for.
如果您只需要对特定方法的工作原理稍加说明,那么“帮助”功能非常方便。
The help function is very handy if all you need is a tiny bit of clarification on how a specific method works.
但是如果你需要更多的帮助,那么你最好的办法就是上网,用谷歌搜索有问题的方法。
But if you need more help, then your best bet is probably to go online and just Google the method in question.


 上一篇
Objects 对象 Objects 对象
1.1.2: Objects 对象Python包含许多数据类型作为核心语言的一部分。Python contains many data types as part of the core language.了解Python程序中的所有数据都
下一篇 
Random Choice 随机选择 Random Choice 随机选择
1.1.5: Random Choice 随机选择通常,当我们使用数字时,偶尔也会使用其他类型的对象,我们希望使用某种类型的随机性。Often when we’re using numbers, but also,occasionally,
  目录