1、类属于模块的一部分。当我们要建立一个类时,通常我们新建一个py文件,例如新建立cn.py,这个cn便成为我们的模块。
2、然后在cn里面建立自己的类:
'''Created on 2011-11-1
@author: dudong0726
'''
class Person:
'''
classdocs
'''
Count = 0
def __init__(self,name,age):
'''
Constructor
@param: name the name of this person
@param: age the age of this person
'''
self.name = name
self.age = age
Person.Count += 1
def detail(self):
'''
the detail infomation of this person
'''
print('name is ',self.name)
print('age is ',self.age)
print('there are '+str(Person.Count)+" person in the class")
3、我们需要在另一个模块中使用这个类,有两种导入方式
1)from cn import * 也就是从cn模块中把所有的东西都导入进来
'''Created on 2011-11-1
@author: dudong0726
'''
from cn import *
if __name__ == '__main__':
p = Person('marry',21)
p.detail()
q = Person('kevin',24)
q.detail()
2)import cn 告诉python我们将要使用这个模块的东西,当我们使用时要在前面加上cn.来指明来自cn这个模块
'''
Created on 2011-11-1
@author: dudong0726
'''
import cn
if __name__ == '__main__':
p = cn.Person('marry',21)
p.detail()
q = cn.Person('kevin',24)
q.detail()
4、我们可以在cn模块中建立一个函数
'''
Created on 2011-11-1
@author: dudong0726
'''
def say(word):
print(word)
class Person:
'''
classdocs
'''
Count = 0
def __init__(self,name,age):
'''
Constructor
@param: name the name of this person
@param: age the age of this person
'''
self.name = name
self.age = age
Person.Count += 1
def detail(self):
'''
the detail infomation of this person
'''
print('name is ',self.name)
print('age is ',self.age)
print('there are '+str(Person.Count)+" person in the class")
5、在另外的模块中调用这个函数
你可以这样调用:
'''
Created on 2011-11-1
@author: dudong0726
'''
from cn import *
if __name__ == '__main__':
p = Person('marry',21)
p.detail()
q = Person('kevin',24)
q.detail()
say("hello world")
当然也可以这样:
'''
Created on 2011-11-1
@author: dudong0726
'''
import cn
if __name__ == '__main__':
p = cn.Person('marry',21)
p.detail()
q = cn.Person('kevin',24)
q.detail()
cn.say("hello world")
分享到:
相关推荐
Python 模块 3_模块导入_import和from_import语句详解和区别.mp4
如果你要导入到python模块与当前模块位于同一目录下,那么只需输入 import,然后是文件名,无需扩展名 .py,代码如下: import useful_functions 注意,useful_functions是你想要导入的本地模块的模块名。 二、示例...
借助Boost.Python库可以将C/C++代码方便、快捷地移植到python模块当中,实现对python模块的扩充。首先,将C++下的代码编译为动态库,并将生成的动态库命名为封装模块的名字,如:用BOOST_PYTHON_MODULE(Module_Name)...
Python 模块 4_import加载底层原理_importlib模块.mp4
import上一级目录的模块 python中,import module会去sys.path搜索,sys.path是个列表,并且我们可以动态修改。 要import某个目录的module,我们sys.path.insert(0,somedir)来加入搜索路径,就可以import了。 既然...
python在不同层级目录import模块的方法.zip
runpy模块用于:在不import python模块时,定位并执行该模块。主要用途在于实现命令行-m执行python 模块得效果,但是是在脚本中而不是文件系统上。 runpy一个就两个函数: runpy.``run_module(mod_name, init_...
ngx_python 是为 nginx 嵌入 python 脚本的模块。环境- python 2.7.*- nginx-1.6.3 安装```sh git clone https://github.com/rryqszq4/ngx_python.git wget 'http://nginx.org/download/nginx-1.6.3.tar.gz' ...
在Python中,import语句用于导入模块或包,以便在当前代码中使用其中的函数、类和变量。本文将介绍Python中import的规则。 1. 模块搜索路径 在Python中,当我们使用import语句导入模块时,Python会按照一定的顺序...
模块安装方法:python setup.py install 使用方法: from goto import goto, label for i in range(1, 10): for j in range(1, 20): for k in range(1, 30): print i, j, k if k == 3: goto .end label .end ...
模块:所谓模块就是一个.py文件,用来存放变量,方法的文件,便于在其他python文件中导入(通过import或from)。 包(package): 包是更大的组织单位,用来组织区别管理多个模块文件。 import 用来导入模块 from 用于从模块...
Python执行import sys语句时,在sys.path变量中所列目录中寻找sys.py模块; 如果找到了这个文件,这个模块的主块中的语句将被运行; 初始化过程仅在第一次输入模块的时候进行; sys模块中的argv变量通过...
在Windows10环境下编写Python3代码,导入AES模块时会提示无法加载: cannot import name '_AES' 原因是因为AES模块中只有Linux版本的代码,并没有Windows64位的模块。 附件压缩包内主要就是增补了Windows64位系统的...
redislite 是一个 Redis 的 Python 模块,用于在 Python 代码中对 Redis 数据进行操作。示例代码:>>> from redislite import Redis >>> redis_connection = Redis('/tmp/redis.db') >>> redis_connection...
自己检查了很多遍,自己写的每错,但是还是报没有找到对应python模块。目录结构如下图所示: __init__.py这个文件需要引入models下的todo_kanban.py文件。__init__内容如下: 写法没问题,但是报错没找到对应模块,...
运行python脚本报错 ImportError: DLL load failed while import cv2: 找不到指定的模块。
import导入其他模块或者函数。文章:https://blog.csdn.net/yysyangyangyangshan/article/details/83005221
python基础_25_import_载入模块_(教学教程tutorial)
模块:本质就是.py结尾的文件(逻辑上组织python代码)模块的本质就是实现一个功能 文件名就是模块名称 包: 一个有__init__.py的文件夹;用来存放模块文件 2、导入模块 import 模块名 form 模块名 import * from ...