博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
forth day——杂记
阅读量:7026 次
发布时间:2019-06-28

本文共 1807 字,大约阅读时间需要 6 分钟。

模块

经常使用有:random,sys,os,math

  • 一个文件即为一个模块
  • py文件的无后缀的文件名就是模块名
  • 使用模块名.变量名就可以在当前文件引用该变量

导入模块

  • 第一种方式:直接导入模块名:import mycode

  • 第二种方式:可以指定导入哪些变量或者函数、类

    from mycode import var,show,A

  • 可以使用*替代明确指定

    from mycode import *

  • 第三种方式,导入后取一个别名

    from mydouble import MyFirstStudentClass as MyClassmyclass = MyClass()myclass.myprint("ok")复制代码
  • 使用路径.语法从包中导入模块

  • 创建目录,放上一个_init_.py文件 这个_init_.py可以为空,也可以为初始化的程序

    有了_init_.py的文件夹叫做模块包

    from module1.module1 import *res = mysub(10,9)print(res)复制代码

装饰器

#装饰器案例DEBUG = Truedef decorator_1(func):	def decorator_nest(*args, **kwargs):		print("调试装饰器")		print("调试状态")		print(args,kwargs)		print("*"*20)		return func(*args, **kwargs)	if DEBUG:		return decorator_nest	else:		return func		def login_required(func):	def decorator_nest(*args, **kwargs):		print("登录装饰器")		if "userid" not in kwargs:			print("您尚未登录,禁止访问")			return None		else:			print("欢迎回来")			return  func(*args,**kwargs)	return decorator_nest		@login_required@decorator_1def myfun(*args,**kwargs):	print(args,kwargs)		myfun("hello","world",userid=10)复制代码

递归&冒泡

递归函数实现斐波拉切数列

def fb2(num):	if num <= 0:		retrun	if num <= 2:		return 1	else:		return fb2(num-1) + fb2(num+1)复制代码

冒泡排序

lists = [10,8,5,6,3,2,4,7,9,1]for i in range(0, len(lists)):	print(lists[i])	for n in range(i, len(lists)):		if lists[i] < lists[n]:			lists[i], lists[n] = lists[n], lists[i]print(lists)复制代码

多态

多态就是同一种行为因为对象不同,执行方式不同

  • issubclass(类1,(类…)——检查类1是不是其他类的子类
  • isinstance(实例,类)——检查某个类是不是其他类的实例

元类

之前创建的类即是object的子类又是object的实例,是一个类类型的实例,object就是一个元类

所有的类其实都是创建的type的实例 type(类名,基本元组类型,包含属性与方法的字典类型)

class A:	passB = type("B", (A,),{
'sex':1})复制代码
  • _new_的第一个参数是类的自身
  • 从type继承
  • 类可以动态创建
  • _new_的方法用于动态创建类
class A(type):	def _new_(cls):		return type("B",(),{
'sex':"BB"}) def _init_(self): print(self) a =A()复制代码
  • _new_的参数从第二个参数开始

转载于:https://juejin.im/post/5c97afecf265da60f5611ada

你可能感兴趣的文章
HTTP Error 503. The service is unavailable
查看>>
常用的排序、查找算法的时间复杂度和空间复杂度
查看>>
Android 检测SD卡状态
查看>>
SQL Server 查询所有包含某文本的存储过程、视图、函数
查看>>
Error response from daemon: conflict: unable to remove repository reference 解决方案
查看>>
【Dijkstra】CCF201609-4 交通规划
查看>>
loadRunner11的安装过程
查看>>
boost-同步-锁选项
查看>>
随机过程(方兆本,缪伯其)读书笔记-第一章-引论
查看>>
Wireless Penetration Testing (1-6 chapter)
查看>>
二分查找
查看>>
大嫂的HTML
查看>>
获取元素的宽高和位置(转自脚本之家)
查看>>
对于yum中没有的源的解决办法-EPEL
查看>>
web安全问题总结
查看>>
使用VMware 管理服务器
查看>>
初学 python 之 用户登录实现过程
查看>>
Spark性能调优
查看>>
BOS中控件非空 非0校验
查看>>
vue入门项目(vue + vuex + VueRouter + mint-ui)
查看>>