![](https://img-blog.csdnimg.cn/20201014180756757.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
笔记
༺༒柒叶༒༻
书中有慧,文笔留香
展开
-
Python基础(实例属性和类属性)
1.类没有实例属性时会调用类属性 class Flower(object): height = 20 def __init__(self,name,color): self.name = name self.color = color f = Flower('玫瑰','红色') print(f.height) #输出:20 2.实例属性的优先级高于类属性 class Flower(object): height = 20 def __init原创 2021-02-25 11:27:14 · 97 阅读 · 0 评论 -
Python基础(定义和实例化类)
1.类的定义 class Flower(object): pass #添加实例属性值 class Flower(object): def __init__(self,name,color,height): #默认值 self.name = name self.color = color self.height = height 2.类的实例化 class Flower(object): def __init__(sel原创 2021-02-24 23:33:53 · 155 阅读 · 0 评论 -
Python基础(内置装饰器)
@property:就像访问属性一样和普通访问少了一个(),将函数名作为一个返回值 被装饰的类方法不可回调 @staticmethod: 可以直接通过类名.函数名直接调用被装饰的方法 @classmethod: 可以直接通过类名.函数名直接调用被装饰的方法 class Myclass(): a=100#类属性 def __init__(self,height,width): self.height=height#实例属性 self.width=width .原创 2021-02-24 23:17:23 · 72 阅读 · 0 评论 -
Python基础(装饰器)
1.定义装饰器 装饰器:我们要增强函数的功能,比如,比如在函数调用前后自动打印日志,但又不希望修改函数的定义, 这种在代码运行期间动态增加功能的方式,称之为‘装饰器’(decorator) #定义装饰器 import time #计算函数运行时间 def run_time(func): def wrapper(): start_time = time.time() func()#调用multiply函数 spend_time = time.time()原创 2021-02-24 23:10:58 · 77 阅读 · 0 评论 -
Python基础(高阶函数)
1.map()函数 #map(函数名,列表/元组/集合/字符串) #map()把传入的函数一次作用于每个元素,处理完后返回的是生成器类型,需要用list生成数据 a = '12345' def square(x): return int(x)*int(x) b = list(map(square,a)) print(b) #输出:[1, 4, 9, 16, 25] 2.filter()函数 #filter(函数名,列表/元组/集合/字符串) #filter()把传入的函数作用在每个元素,然后根据返原创 2021-02-24 22:51:06 · 62 阅读 · 0 评论 -
Python基础(函数的递归和回调)
1.函数的递归 #阶乘计算 # 必须设置函数终止条件 # 使用递归函数的优点是逻辑简单清晰,缺点是过深的调用会导致栈溢出 def fact(n): if n == 1: return 1 return n * fact(n - 1) print(fact(4))#24 2.函数的回调 # 通过向函数里传入已定义的不同功能的函数名来使用不同的功能函数 def add(x,y): return x + y def multiply(x,y): return x原创 2021-02-24 22:40:04 · 88 阅读 · 0 评论 -
Python基础(函数参数)
1.必选参数parameter def detail1(name,age,tel): print('姓名:',name) print('年龄:',age) print('电话:',tel) detail1('qiye',12,123456) 2.默认参数parameter = value def detail2(name,age,tel,gender = 'fale'):#默认参数必须放必须参数后面 print('姓名:',name) print('性别:',gen原创 2021-02-24 22:32:20 · 82 阅读 · 0 评论 -
Python基础(函数的定义)
#定义函数: def myfunc(x): if x >= 0: return x else: return -x #空函数: def emptyfunc(): pass#占位符 #参数检查: def checkfunc(x): if not isinstance(x,(int,float)): raise TypeError('must bi int or float type!') if x >= 0:原创 2021-02-24 22:20:30 · 74 阅读 · 0 评论 -
Python基础(流程控制)
1.if判断 ''' if else 语法: if 条件: 语句 else: 语句 例子: a = 1 #方法一 if a > 1: print('大于1') else: print('小于等于1') #手机用方法二(三目运算法) print('大于1') if a > 1 else print('小于等于1') 输出: 小于等于1 小于等于1 if elif else 语法: if 条件: 语句 elif: 语句 else:原创 2021-02-24 21:57:28 · 87 阅读 · 0 评论 -
Python基础(数据类型)
**1.数据类型 1.1类型 基本数据类型: 整形(int),浮点型(float),字符串(str),列表(list),元组(tuple),字典(dict),集合(set),布尔(bool) 数值类型(整数,浮点,布尔) 序列类型(字符串,列表,元组) 散列类型(字典,集合) 字节类型 a = bytes(‘123’)/a = b’123’ 字节数组 bytearrary(‘123’) 可变序列:列表,集合,字典 不可变序列:字符串,元组 1.2方法 1.2.1字符串方法 增: 字符串拼接 1.str1 +原创 2021-02-24 20:54:57 · 97 阅读 · 0 评论