search
Homephp教程PHP源码Python创建模块及模块导入的方法_python

Python创建模块及模块导入的方法_python

May 26, 2016 am 08:19 AM
pythonmodule import

这篇文章主要介绍了Python创建模块及模块导入的方法,实例分析了模块的定义、导入及模块属性的使用技巧,并附带说明了包的概念与用法,需要的朋友可以参考下

本文实例讲述了Python创建模块及模块导入的方法。分享给大家供大家参考。具体分析如下:

python学习手册中写道:

定义模块,只要使用文本编辑器,把一些python代码输入到文本中,然后以.py为后缀名进行保存,任何此类文件都会被认为是python模块。

比如说,下面的代码输入到一个文件中,就可以看作是一个模块:

def printme(var):
  print var
if __name__ == '__main__':
    printme(1)

假设说输入到a.py中,那么import a就可以把这个模块导入。

然后可执行a.printme(3),屏幕即可打印出3:

>>> a.printme(3)
3
>>>

一个模块顶层定义的变量,会自动变成模块的属性。例如:

data=[1,2,3]
def printme(var):
  print var
if __name__ == '__main__':
    printme(1)

data变量就是模块的一个属性。其实printme也是一个属性,只不过是一个函数罢了。

引入模块示例如下:(假定此时data=[1,2,3]未定义)

>>> import a
>>> a.data

Traceback (most recent call last):
 File "<pyshell#1>", line 1, in <module>
  a.data
AttributeError: &#39;module&#39; object has no attribute &#39;data&#39;
>>> reload(a)
<module &#39;a&#39; from &#39;C:/py\a.pyc&#39;>
>>> a.data

Traceback (most recent call last):
 File "<pyshell#3>", line 1, in <module>
  a.data
AttributeError: &#39;module&#39; object has no attribute &#39;data&#39;
>>>

从上述提示可以看出data属性未定义,此时再在a.py文件中定义data=[1,2,3],重新加载a模块,并输出data属性:

>>> reload(a)
<module &#39;a&#39; from &#39;C:/py\a.py&#39;>
>>> a.data
[1, 2, 3]
>>>

这里的reload函数可以重新加载一个模块。如果在模块代码中更改了,那么需要重新加载。

上面a.data,就是访问模块中的属性。

上面的例子是导入一个文件作为一个模块。

其实python的模块导入还有更丰富的内容。

除了模块名之外,python也可以导入指定目录路径。python代码的目录就称为包。因此,这类导入就称为包导入。事实上,包导入是把计算机上的目录变成python的一个命名空间。而属性就是目录中包含的子目录或者是模块文件。

看下面例子:

在我的桌面上有一个aa文件夹,里面有bb文件夹,bb里面有a.py这个文件。

那么在aa和bb文件夹中分别放置一个__init__.py,之后,在命令行中import aa.bb.a,就可以导入模块a了。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software