python中的模块_Python中的模块

python中的模块

A set of related code is known to be a

一组相关代码被称为

Module, it helps us to organize our code logically which is much easier for us to understand and use it.

Module ,它帮助我们逻辑上组织代码,这对我们来说更容易理解和使用它。

Module is an object with arbitrarily named attributes which can be used in binding and referencing.

模块是一个具有任意命名属性的对象,可用于绑定和引用。

We can define a function, classes and variables inside a module; it can also have the runnable code.

我们可以在模块内部定义函数,类和变量; 它也可以具有可运行的代码。

Example: There is a function called "FLOOR" which is used to round the value for the given input to it. This function is inside a module that allows us to use it.

示例:有一个名为“ FLOOR ”的函数,该函数用于将给定输入的值四舍五入。 此功能位于允许我们使用它的模块中。

Let us check if we are able to use this function or not:

让我们检查一下我们是否能够使用此功能:

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> floor(17.5)

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    floor(17.5)
NameError: name 'floor' is not defined
>>>  

Let us import the module i.e. "MATH" which has "FLOOR" function.

让我们导入具有“ FLOOR”功能的模块,即“ MATH”。

>>> import math
>>> floor(17.5)
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    floor(17.5)
NameError: name 'floor' is not defined
>>> math.floor(17.5)
17.0
>>> 

FLOOR" function.

地板 ”功能。

To check what are the other modules present in the math module we can hit ">>> math." and hold for a sec it will display all the functions associated with it. Or by using "dir()" which is a built-in function which will return the list of the names defined inside a module as like below:

要检查math模块中还有哪些其他模块,我们可以点击“ >>> math。 ”并保持一秒钟,它将显示与之关联的所有功能。 或者使用内置函数“ dir() ”,它将返回模块内部定义的名称列表,如下所示:

>>> dir(math)
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
>>> 

Now let us see how we can assign it to an variable, so that we don’t have to use " module.function<<agruments>> " every time.

现在让我们看看如何将其分配给变量,这样就不必使用“ module.function << agruments >> “每次。

Example: 
>>> var1 =math.floor
>>> var1(17.5)
17.0
>>> 

var1" variable in our code instead of writing "math.floor" every time.

在我们的代码中使用var1 “变量,而不是每次都写” math.floor “。

We can also created our own module where we can keep specific function and variables that we have declared or want to make use of the same on other scripts , and also we can keep that to use the same in our future reference.  Like when we close or exit Python IDLE the variables and functions which we have use will be lost unless we save them in some file or as a script.

我们还可以创建自己的模块,在其中可以保留已声明或希望在其他脚本上使用的特定函数和变量,也可以在以后的引用中将其保留为相同的功能。 就像当我们关闭或退出Python IDLE时一样,除非我们将它们保存在某个文件中或作为脚本保存,否则所使用的变量和函数将丢失。

So the best way is to create a module and keep them and use in future as per our need. It is also helpful when we create a function and have it on the module, so that we can make use of the same functions multiple times.

因此,最好的方法是创建一个模块,并根据我们的需要保留它们并在将来使用。 当我们创建一个函数并将其放在模块上时,它也很有用,这样我们可以多次使用相同的函数。

In order to create modules navigate to File on Python IDLE and then click on New Window or you can use the shortcut Ctrl+N.

为了创建模块,请导航至Python IDLE上的File,然后单击New Window,或者可以使用快捷键Ctrl + N。

The below code does nothing bust just to print a line which is very basic and a simple.

下面的代码只是打印一条非常基本且简单的行,所以不会做任何破坏。

    def module1():
    print " It really worked "
Simple example of a module

Now click on “Save As” option under “File”. Save it on the same directory where Python is installed.

现在单击“文件”下的 另存为”选项。 将其保存在安装Python的同一目录中。

For me it’s on: C:\Python27

对我来说,它在: C:\ Python27

Save the module on directory where Python is installed

Now give any name to your script with python extension.

现在使用python扩展名给脚本命名。

Let us give as “mod1.py”

让我们给出“ mod1.py”

Give a name and save the file

Now click ok “Save”.

现在单击确定“保存”。

Note: Always remember the name of the file which is your module name.

注意:请始终记住文件名,即您的模块名。

Now go to Python IDLE and import the module.

现在转到Python IDLE并导入模块。

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import mod1

Now once imported we need to call the module for which we need to call the object or else we can directly call like as below:

现在,一旦导入,我们需要调用需要为其调用对象的模块,否则我们可以像下面这样直接调用:

>>> mod1.module1()
 It really worked 
>>>

For getting more information on modules in PYTHON we can go to Python official website i.e. http://docs.python.org/tutorial/modules.html#

要获取有关PYTHON中模块的更多信息,我们可以访问Python官方网站,即http://docs.python.org/tutorial/modules.html#

翻译自: https://www.experts-exchange.com/articles/10572/Modules-in-Python.html

python中的模块

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值