随笔分类 - python基础
摘要:""" __init__ 构造方法, 类()自动执行 __del__ 析构方法,对象在被销毁时执行的方法 __call__ 对象() 类()() 自动执行 __int__ int(对象) __str__ str() """ class FOO: def __init__(self): self.na
阅读全文
摘要:""" 公有成员 私有成员, __字段名 - 无法直接访问,只能间接访问 """ class F: def __init__(self): self.name = "aaa" # 公有变量 self.__age = 123 # 私有变量 def __f(self): # 私有方法 pass clas
阅读全文
摘要:class paging: def __init__(self, current_page): try: p = int(current_page) except Exception as e: p = 1 self.page = p @property def start(self): val =
阅读全文
摘要:类成员:# 字段- 普通字段,保存在对象中,执行只能通过对象访问- 静态字段,保存在类中, 执行可以通过对象访问也可以通过类访问# 方法- 普通方法,保存在类中,由对象来调用,self =》对象- 静态方法,保存在类中,由类直接调用- 类方法,保存在类中,由类直接调用,cls =》当前类 class
阅读全文
摘要:import time dic = {} Controls = {"1": "查看角色", "2": "修炼", "3": "战斗"} class game: def __init__(self, n, g, s): self.name = n self.age = g self.sex = s d
阅读全文
摘要:1、如何创建类 class 类名: pass 2、创建方法 构造方法,__init__(self,arg) obj = 类('a1') 普通方法 obj = 类(‘xxx’) obj.普通方法名() 3、面向对象三大特性之一:封装 class Bar: def __init__(self, n,a)
阅读全文
摘要:import xml.etree.ElementTree as ET tree = ET.parse("test.xml") root = tree.getroot() print(root.tag) # 遍历xml文档 for child in root: print(child.tag, chi
阅读全文
摘要:JSON和pickle区别在于: JSON不能转换函数类等,但pickle可以进行转换,并且pickle也支持字典、列表等类型 JSON格式可以全语言通用方便阅读查看,pickle格式只支持python使用 shelve跟pickle类似,但shelve可以生成一个字典对象,根据字典对象进行操作 i
阅读全文
摘要:import re # 正则表达式中的元字符: # “.” 点通配符表示可以替换表达式中的任意字符,只能代指一个字符,除换行符外 print(re.findall("a..", "hdhgaqwe")) # “^”只从开始匹配 print(re.findall("^a..", "ahdhgaqwe"
阅读全文
摘要:import configparser config = configparser.ConfigParser() ''' # 创建配置文件 config["DEFAULT"] = {"ServerAliveInterval": "45", "Compression": "yes", "Compres
阅读全文
摘要:import logging # 第一种方式,只能选择控制台输出或文件输出日志 # logging.basicConfig(level=logging.DEBUG, # format="%(asctime)s %(filename)s %(lineno)d %(levelname)s %(messa
阅读全文
摘要:# 加密模块 import hashlib m = hashlib.md5() # 创建MD5加密方法的对象 m.update("123".encode("utf-8")) # 加密内容,因为Python3默认编码方式是Unicode,所以需要转码成UTF-8 print(m.hexdigest()
阅读全文
摘要:import random print(random.random()) # 0-1的随机数 print(random.randint(1, 10)) # 指定范围整数的随机数 print(random.randrange(1, 10)) # range生成序列的随机数 print(random.c
阅读全文
摘要:import time import datetime print(time.time()) # 时间戳格式 time.sleep(1) # 延迟时间 print(time.localtime()) # 北京时区元组格式时间,结构时间 print(time.strftime("%Y-%m-%d %H
阅读全文
摘要:from collections.abc import Iterator, Iterable # 生成器是一个迭代器,迭代器不一定是生成器 # 迭代器满足两个条件:1、有iter方法,2、可以被next方法调用 l = [1, 2, 3, 5] d = iter(l) print(d) print(
阅读全文
摘要:def consumer(name): print("%s 开始购买物品了!" % name) while True: item_name = yield print("物品 %s 被 %s 购买了!" % (item_name, name)) def producer(name): c = con
阅读全文
摘要:# 把a创建成了一个生成器对象 generator object a = (x * 2 for x in range(10)) print(a) print(next(a)) # 生成器对象调用用next(a),等价于 a.__next__(),生成器一次调用一个 print(next(a)) fo
阅读全文
摘要:# 获取文件内容 def open_text(login_text): with open(login_text, "r", encoding="UTF-8") as file: file = eval(file.read()) return file login_flag = False # 登录
阅读全文
摘要:import time # 装饰器函数 def log_cat(flag='false'): # 加一层可以进行参数传递 def time_def(f): def inner(*args): start = time.time() f(*args) end = time.time() s_e = e
阅读全文
摘要:# 局部变量不能修改全局变量,如需要修改是需要先进行声明 a = 1 # 全局变量 def f(): global a # 声明使用全局变量 print("第1次打印:%s" % a) a = 2 # 修改全局变量 print("第2次打印:%s" % a) def f_1(): global a
阅读全文

浙公网安备 33010602011771号