Fork me on GitHub
摘要: 总结了一些常见的 文件 和目录的操作。 os模块 import os print '***获取当前目录***') print os.getcwd() print os.path.abspath(os.path.dirname(__file__)) print '***获取上级目录***' print 阅读全文
posted @ 2021-12-30 11:04 橘子偏爱橙子 阅读(62) 评论(0) 推荐(0) 编辑
摘要: 定义:Python中,对日期和时间的操作,主要使用这3个内置模块: datetime 、 time 和 calendar1、某个函数执行大概耗费了多少时间,就可以使用time.time()来做。获取时间差 def fun1(): pass import time before=time.time() 阅读全文
posted @ 2021-12-29 18:54 橘子偏爱橙子 阅读(62) 评论(0) 推荐(0) 编辑
摘要: 一、快速安装 执行下面这些命令进行安装 PySnooper $ python3 -m pip install pysnoopere 二、快速使用 1、 定义了一个 demo_func 的函数,在里面生成一个 profile 的字典,更新字典值,最后返回 import pysnooper @pysno 阅读全文
posted @ 2021-12-29 15:05 橘子偏爱橙子 阅读(59) 评论(0) 推荐(0) 编辑
摘要: 一、类的封装(Encapsulation) 定义:封装是指将数据与具体操作的实现代码放在某个对象内部,使这些代码的实现细节不被外界发现,外界只能通过接口使用该对象,而不能通过任何形式修改对象内部实现 示例: class Person: def __init__(self, name, age): s 阅读全文
posted @ 2021-12-28 22:36 橘子偏爱橙子 阅读(64) 评论(0) 推荐(0) 编辑
摘要: 一. 写法上的差异 类的方法可以分为: 实例方法:没有任何装饰器的普通函数 静态方法:有 staticmethod 装饰的函数 类方法:有 classmethod 装饰的函数 举个例子,如下这段代码中,run 普通的实例方法,eat 是静态方法,jump 是类方法。 class Animal: #初 阅读全文
posted @ 2021-12-25 20:09 橘子偏爱橙子 阅读(121) 评论(0) 推荐(0) 编辑
摘要: 1、如何定义类? 下边我定义了一个 Animal 的类 class Animal: age = 0 def __init__(self, name): self.name = name def run(self): print(f"{self.name} 跑起来了") 其中 Animal 是类名 _ 阅读全文
posted @ 2021-12-25 18:34 橘子偏爱橙子 阅读(36) 评论(0) 推荐(0) 编辑
摘要: 一、如何抛出异常? 异常的产生有两种来源: 一种是程序自动抛出,比如 1/0 会自动抛出 ZeroDivisionError 一种是开发者主动抛出,使用 raise 关键字抛出。 示例: def demo_func(filename): if not os.path.isfile(filename) 阅读全文
posted @ 2021-12-24 15:16 橘子偏爱橙子 阅读(161) 评论(0) 推荐(0) 编辑
摘要: 一、匿名函数的使用 匿名函数(英语:anonymous function)是指一类无需定义标识符(函数名)的函数。通俗来说呢,就是它可以让我们的函数,可以不需要函数名 这边使用def 和 lambda 分别举个例子,你很快就能理解 def mySum(x, y): return x+y mySum( 阅读全文
posted @ 2021-12-22 14:36 橘子偏爱橙子 阅读(40) 评论(0) 推荐(0) 编辑
摘要: 函数的参数 形参和实参 所谓形参,就是函数定义中的参数,形参在函数体内可以使用,而实参,则出现在调用过程中 def print_diamond(count): for i in range(count): print(i) print_diamond(11) 上面的代码中,函数定义中的count就是 阅读全文
posted @ 2021-12-20 17:12 橘子偏爱橙子 阅读(38) 评论(0) 推荐(0) 编辑
摘要: 函数是一种仅在调用时运行的代码块。您可以将数据(称为参数)传递到函数中,然后由函数可以把数据作为结果返回。 1. 函数的定义 def 函数名(参数列表): 函数体 在 Python 中,使用 def 关键字定义函数 def hello_word(): pass def print_diamond(c 阅读全文
posted @ 2021-12-20 15:50 橘子偏爱橙子 阅读(74) 评论(0) 推荐(0) 编辑