摘要:
# ###__bool__ 魔术方法 ''' 触发时机:使用bool(对象)的时候自动触发 功能:强转对象 参数:一个self接受当前对象 返回值:必须是布尔类型 ''' ''' 类似的还有如下等等(了解): __complex__(self) 被complex强转对象时调用 __int__(self) 被int强转对象时调用 __float__(sel... 阅读全文
posted @ 2019-05-24 22:58
MR_黄Python之路
阅读(262)
评论(0)
推荐(0)
摘要:
# ### __call__ 魔术方法 ''' 触发时机:把对象当作函数调用的时候自动触发 功能: 模拟函数化操作 参数: 参数不固定,至少一个self参数 返回值: 看需求 ''' # (1)基本语法 class MyClass(): def __call__(self): print("__call__方法被调用") return "done" obj = MyCla... 阅读全文
posted @ 2019-05-24 22:57
MR_黄Python之路
阅读(389)
评论(0)
推荐(0)
摘要:
# ### __str__ ''' 触发时机: 使用print(对象)或者str(对象)的时候触发 功能: 查看对象 参数: 一个self接受当前对象 返回值: 必须返回字符串类型 ''' class Cat(): gift = "抓老鼠" def __init__(self,name): self.name = name def cat_info(... 阅读全文
posted @ 2019-05-24 22:57
MR_黄Python之路
阅读(383)
评论(0)
推荐(0)
摘要:
# ### __del__ 析构方法 __init__ 构造方法 ''' 触发时机:当对象被内存回收的时候自动触发[1.页面执行完毕回收所有变量 2.所有对象被del的时候] 功能:对象使用完毕后资源回收 参数:一个self接受对象 返回值:无 ''' class LangDog(): def __init__(self,name): self.name = name ... 阅读全文
posted @ 2019-05-24 22:56
MR_黄Python之路
阅读(241)
评论(0)
推荐(0)
摘要:
# ### 单态模式 '''无论实例化多少次,都有且只有一个对象.''' '''最终目的:为了节省内存空间.应用的场景是只调用相关的成员属性或方法,而不用动态添加成员属性方法的环境中''' """ __obj 变成私有,控制不让用户在类外直接获取obj,而是用__new__来控制返回的逻辑 先判断cls.__obj 是不是None 如果是None,代表是一个空的对象,那么就利用父类的__ne... 阅读全文
posted @ 2019-05-24 22:51
MR_黄Python之路
阅读(209)
评论(0)
推荐(0)
摘要:
# 1.类或对象是否能做字典的key # 2.简述python的私有成员是如何实现的 # 3.私有成员能在类的外部使用么?能在子类中使用么? # 4.读程序写结果.(不执行) ''' class StarkConfig(object): def __init__(self,num): self.num = num def changelist(self,reque... 阅读全文
posted @ 2019-05-24 22:50
MR_黄Python之路
阅读(177)
评论(0)
推荐(0)
摘要:
# ### __new__ ''' 触发时机:实例化类生成对象的时候触发(触发时机在__init__之前) 功能:控制对象的创建过程 参数:至少一个cls接受当前的类,其他根据情况决定 返回值:通常返回对象或None ''' # (1) 基本用法 """ python3.x 新式类 python2.x 旧式类, 新式类不需要每次都写一次object,默认继承 作用:控制创建的对象, ""... 阅读全文
posted @ 2019-05-24 22:50
MR_黄Python之路
阅读(452)
评论(0)
推荐(0)
摘要:
# ### 菱形继承 ''' Human Man Woman Children ''' class Human(): pty = 111 def feelT(self): print("远古人类天热了,脱毛1") print(self.pty) print("远古人类天冷了,长毛2") class Man(Human): pty = 222 ... 阅读全文
posted @ 2019-05-24 22:41
MR_黄Python之路
阅读(216)
评论(0)
推荐(0)
摘要:
# ### 多继承 # (1) 基本语法 class Father(): f_property = "风流倜傥,英俊潇洒" def f_hobby(self): print("吃喝嫖赌抽,坑蒙拐骗偷,喜欢烫头") class Mother(): m_property = "沉鱼落雁,闭月羞花" def m_hobby(self): print("打麻将,跳广场舞,喜欢买... 阅读全文
posted @ 2019-05-24 22:40
MR_黄Python之路
阅读(203)
评论(0)
推荐(0)
摘要:
# ### 继承 : 一个类除了拥有自身的属性方法之外,还拥有另外一个类的属性和方法 ''' 继承: 1.单继承 2.多继承 子类:一个类继承了另外一个类,那么这个类是子类,(衍生类) 父类:一个类继承了另外一个类,被继承的那个类是父类 (超类,基类) object 是所有的类的父类 ''' class Human(): def eat(self): print("远古人类打猎,吃猎物... 阅读全文
posted @ 2019-05-24 22:36
MR_黄Python之路
阅读(140)
评论(0)
推荐(0)
摘要:
### 1.完成下列功能: # 1.1创建一个人类Person,再类中创建3个成员属性 # animal = '高级动物' # soul = '有灵魂' # language = '语言' class Person(): animal='高级动物' soul ='有灵魂' language ='语言' obj =Person() print(Person.ani... 阅读全文
posted @ 2019-05-24 22:35
MR_黄Python之路
阅读(275)
评论(0)
推荐(0)
摘要:
# ### __init__ 魔术方法 ''' 触发时机:实例化对象,初始化的时候触发 功能:为对象添加成员,用来初始化的 参数:参数不固定,至少一个self参数 返回值:无 ''' # (1)基本用法 class MyClass(): def __init__(self): # print(1111) self.name = "张国成" # 实例化对象 [类的实例化] ... 阅读全文
posted @ 2019-05-24 22:34
MR_黄Python之路
阅读(265)
评论(0)
推荐(0)
摘要:
# ### 如何访问私有成员 class Plane(): # 公有属性 captain = "马军强" # 私有属性 __air_sister = "20个" # 公有绑定方法 def fly(self): print("飞机会飞") # 公有普通方法 def fly2(): print("飞机会飞2") # 私有的绑定方法 def __oil_info... 阅读全文
posted @ 2019-05-24 22:33
MR_黄Python之路
阅读(1086)
评论(0)
推荐(0)
摘要:
# ### 类的相关操作 class MyCar(): oil = "涡轮增压发动机1.5T" __price = "100万" # 公有普通方法 (只能类来调用) def oil_info(): # 类.oil print("我的油耗信息:" +MyCar.oil) # 私有普通方法 def __price_info(): print("我的价格是保密的") ... 阅读全文
posted @ 2019-05-24 22:32
MR_黄Python之路
阅读(278)
评论(0)
推荐(0)
摘要:
# ### 类的封装性 ''' 公有的,在类外可以调用类的相关共有属性方法 私有的(前面开头加上__ 两个下划线),在外类不可以调用类内的相关私有属性方法 绑定方法: (1) 绑定到对象 (默认系统把对象当成参数传递) (2) 绑定到类 (默认系统把类当成参数传递) 这两个参数,无论哪种,都是系统自己传递的, 但是参数需要我们自己定义好 ''' class Car(): # 公有... 阅读全文
posted @ 2019-05-24 22:31
MR_黄Python之路
阅读(359)
评论(0)
推荐(0)
摘要:
# ### oop 面向对象程序开发 # (1) 类的定义 # 1. class MyClass: pass # 2.推荐 class MyClass(): pass # 3. class MyClass(object): pass # (2) 类的实例化 class MyClass(): pass # 类的实例化,实例化对象 obj = MyClass() # ob... 阅读全文
posted @ 2019-05-24 22:29
MR_黄Python之路
阅读(317)
评论(0)
推荐(0)
摘要:
# ### tarfile 压缩模块 import tarfile # (1)创建tarfile 压缩包 tf = tarfile.open("ceshi001.tar","w",encoding="utf-8") # add(路径,别名) 添加文件到压缩包当中 tf.add("/bin/dash","dash") tf.add("/bin/dd","dd") tf.add("/bin/df",... 阅读全文
posted @ 2019-05-24 22:28
MR_黄Python之路
阅读(548)
评论(0)
推荐(1)
摘要:
# ### os 和 shutil 模块 ''' 文件操作领域: os => 新建和删除 shutil => 复制和剪切 ''' import os # 默认更改工作路径 os.chdir("/home/wangwen/mywork") #os.mknod 创建文件 # os.mknod("ceshi1014.txt") #os.remove 删除文件 # os.remove("... 阅读全文
posted @ 2019-05-24 22:26
MR_黄Python之路
阅读(347)
评论(0)
推荐(0)
摘要:
# ### 压缩模块 zipfile # (1) 创建一个zip压缩包 import zipfile # zip_deflated 代表是压缩的意思 # 打开压缩包 zf = zipfile.ZipFile("ceshi1136.zip","w",zipfile.ZIP_DEFLATED) print(zf) # 写入文件 # write("路径","别名") zf.write("/bin/... 阅读全文
posted @ 2019-05-24 22:26
MR_黄Python之路
阅读(509)
评论(0)
推荐(0)
摘要:
# ### 计算任意文件夹的大小 import os ''' pathvar = "/mnt/hgfs/gongxiang_16/day17/ceshi100" lst = os.listdir(pathvar) print(lst) # 遍历所有的文件和文件夹,计算文件的大小 size = 0 for i in lst: # print(i) path_new = os.path.j... 阅读全文
posted @ 2019-05-24 22:25
MR_黄Python之路
阅读(334)
评论(0)
推荐(0)
摘要:
# ### os.path import os #abspath() 将相对路径转化为绝对路径 *** res = os.path.abspath(".") print(res) #basename() 返回文件名部分 *** pathvar = "/mnt/hgfs/gongxiang_16/day16/2.py" res = os.path.basename(pathvar) pr... 阅读全文
posted @ 2019-05-24 22:21
MR_黄Python之路
阅读(248)
评论(0)
推荐(0)
摘要:
# ### os 对系统进行操作 import os #system() 在python中执行系统命令 # os.system("touch ceshi1.txt") #linux # os.system("ifconfig") # os.system("mspaint") # windows # os.system("ipconfig") #popen() 执行系统命令返回对象,通过... 阅读全文
posted @ 2019-05-24 22:18
MR_黄Python之路
阅读(252)
评论(0)
推荐(0)
摘要:
# ### time 时间模块 import time #time() 获取本地时间戳 res = time.time() print(res) #mktime() 通过[时间元组]获取[时间戳] (参数是时间元组) ttp = (2019,5,16,10,55,30,0,0,0) res = time.mktime(ttp) print(res) #localt... 阅读全文
posted @ 2019-05-24 22:17
MR_黄Python之路
阅读(195)
评论(0)
推荐(0)
摘要:
# ### math 数学模块 import math #ceil() 向上取整操作 (对比内置round) res = math.ceil(6.0001) # 注意精度损耗 print(res) #floor() 向下取整操作 (对比内置round) res = math.floor(3.5) res = math.floor(3.9999999) print(res) #pow() ... 阅读全文
posted @ 2019-05-24 22:16
MR_黄Python之路
阅读(209)
评论(0)
推荐(0)
摘要:
# ### random 随机模块 import random #random() 获取随机0-1之间的小数(左闭右开) res = random.random() # 0 当取0时 4 4+-6*(0~1) => 当取1时 -2 (1是取不到的) 所以: -2 < x <=4 ''' #choice() 随机获取序列中的值(多选一) listvar = ["周杰伦","王文","... 阅读全文
posted @ 2019-05-24 22:16
MR_黄Python之路
阅读(149)
评论(0)
推荐(0)
摘要:
# ### json """ json 模块能够转化的数据类型如下: int float bool str list tuple dict None 8个数据类型可以序列化 json数据类型的提出,是让不同的语言之间形成数据交流 pickle返回的是二进制的字节流,它是用来进行数据的传输和存储的. json 序列化成一个字符串 pickle 序列化成一个字节流 """ import jso... 阅读全文
posted @ 2019-05-24 22:15
MR_黄Python之路
阅读(333)
评论(0)
推荐(0)
摘要:
# ### pickle 序列化模块 ''' 把不能够直接存储的数据,变得可存储就是序列化 把存储好的数据,转化成原本的数据类型,叫做反序列化 php: 序列化与反序列化 serialize unserialize ''' # 导入pickle 模块 => 模块.方法() import pickle #dumps 把任意对象序列化成一个bytes lst = [1,2,3,4,4,5] r... 阅读全文
posted @ 2019-05-24 22:14
MR_黄Python之路
阅读(242)
评论(0)
推荐(0)
摘要:
# abs 绝对值函数 intvar = -9 res = abs(intvar) print(res) # round 四舍五入 (n.5 n为偶数则舍去 n.5 n为奇数,则进一!) '''奇进偶不进 , 只在n.5的情况下发生''' a = 5.34 a = 6.5 a = 5.5 a = 6.56 a = 17.51 print(round(a)) # sum 计算... 阅读全文
posted @ 2019-05-24 22:13
MR_黄Python之路
阅读(192)
评论(0)
推荐(0)
摘要:
### python3.6.x在Ubuntu16.04下安装过程 ``` #(1)保证网络正常连接 sudo add-apt-repository ppa:jonathonf/python-3.6 (如果超时,在运行一次) sudo apt-get update (更新软件列表,拿取最新资源) sudo apt-get install python3.6 (安装pyth... 阅读全文
posted @ 2019-05-24 22:08
MR_黄Python之路
阅读(242)
评论(0)
推荐(0)
摘要:
def extendList(val, list=[]): list.append(val) return list list1 = extendList(10) print(list1) #[10] list2 = extendList(123, []) print(list2) #[123] list3 = extendList('a') print(list3) #... 阅读全文
posted @ 2019-05-24 22:06
MR_黄Python之路
阅读(229)
评论(0)
推荐(0)
摘要:
""" #复习 lst1 = [1,5] lst2 = [2,6] lst3 = [3,7] lst4 = [4,8] it = zip(lst1,lst2,lst3,lst4) print(list(it)) # [(1, 2), (3, 4), (5, 6), (7, 8)] # n = 2 lst1 = [1 , 3 , 5 , 7 ,9] listvar[0::2] # 0 2 4... 阅读全文
posted @ 2019-05-24 22:05
MR_黄Python之路
阅读(227)
评论(0)
推荐(0)
浙公网安备 33010602011771号