随笔分类 -  python基础

摘要:""" __init__ 构造方法, 类()自动执行 __del__ 析构方法,对象在被销毁时执行的方法 __call__ 对象() 类()() 自动执行 __int__ int(对象) __str__ str() """ class FOO: def __init__(self): self.na 阅读全文
posted @ 2024-12-18 17:51 GDquicksand 阅读(7) 评论(0) 推荐(0)
摘要:""" 公有成员 私有成员, __字段名 - 无法直接访问,只能间接访问 """ class F: def __init__(self): self.name = "aaa" # 公有变量 self.__age = 123 # 私有变量 def __f(self): # 私有方法 pass clas 阅读全文
posted @ 2024-12-18 17:51 GDquicksand 阅读(17) 评论(0) 推荐(0)
摘要: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 = 阅读全文
posted @ 2024-12-10 18:43 GDquicksand 阅读(10) 评论(0) 推荐(0)
摘要:类成员:# 字段- 普通字段,保存在对象中,执行只能通过对象访问- 静态字段,保存在类中, 执行可以通过对象访问也可以通过类访问# 方法- 普通方法,保存在类中,由对象来调用,self =》对象- 静态方法,保存在类中,由类直接调用- 类方法,保存在类中,由类直接调用,cls =》当前类 class 阅读全文
posted @ 2024-12-10 18:42 GDquicksand 阅读(15) 评论(0) 推荐(0)
摘要: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 阅读全文
posted @ 2024-12-09 17:52 GDquicksand 阅读(6) 评论(0) 推荐(0)
摘要:1、如何创建类 class 类名: pass 2、创建方法 构造方法,__init__(self,arg) obj = 类('a1') 普通方法 obj = 类(‘xxx’) obj.普通方法名() 3、面向对象三大特性之一:封装 class Bar: def __init__(self, n,a) 阅读全文
posted @ 2024-12-09 16:49 GDquicksand 阅读(7) 评论(0) 推荐(0)
摘要: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 阅读全文
posted @ 2024-11-26 17:50 GDquicksand 阅读(9) 评论(0) 推荐(0)
摘要:JSON和pickle区别在于: JSON不能转换函数类等,但pickle可以进行转换,并且pickle也支持字典、列表等类型 JSON格式可以全语言通用方便阅读查看,pickle格式只支持python使用 shelve跟pickle类似,但shelve可以生成一个字典对象,根据字典对象进行操作 i 阅读全文
posted @ 2024-11-22 18:03 GDquicksand 阅读(12) 评论(0) 推荐(0)
摘要:import re # 正则表达式中的元字符: # “.” 点通配符表示可以替换表达式中的任意字符,只能代指一个字符,除换行符外 print(re.findall("a..", "hdhgaqwe")) # “^”只从开始匹配 print(re.findall("^a..", "ahdhgaqwe" 阅读全文
posted @ 2024-11-12 16:45 GDquicksand 阅读(8) 评论(0) 推荐(0)
摘要:import configparser config = configparser.ConfigParser() ''' # 创建配置文件 config["DEFAULT"] = {"ServerAliveInterval": "45", "Compression": "yes", "Compres 阅读全文
posted @ 2024-11-08 17:37 GDquicksand 阅读(9) 评论(0) 推荐(0)
摘要:import logging # 第一种方式,只能选择控制台输出或文件输出日志 # logging.basicConfig(level=logging.DEBUG, # format="%(asctime)s %(filename)s %(lineno)d %(levelname)s %(messa 阅读全文
posted @ 2024-09-24 10:01 GDquicksand 阅读(14) 评论(0) 推荐(0)
摘要:# 加密模块 import hashlib m = hashlib.md5() # 创建MD5加密方法的对象 m.update("123".encode("utf-8")) # 加密内容,因为Python3默认编码方式是Unicode,所以需要转码成UTF-8 print(m.hexdigest() 阅读全文
posted @ 2024-09-24 09:59 GDquicksand 阅读(11) 评论(0) 推荐(0)
摘要:import random print(random.random()) # 0-1的随机数 print(random.randint(1, 10)) # 指定范围整数的随机数 print(random.randrange(1, 10)) # range生成序列的随机数 print(random.c 阅读全文
posted @ 2024-09-14 17:44 GDquicksand 阅读(22) 评论(0) 推荐(0)
摘要:import time import datetime print(time.time()) # 时间戳格式 time.sleep(1) # 延迟时间 print(time.localtime()) # 北京时区元组格式时间,结构时间 print(time.strftime("%Y-%m-%d %H 阅读全文
posted @ 2024-09-14 17:43 GDquicksand 阅读(16) 评论(0) 推荐(0)
摘要:from collections.abc import Iterator, Iterable # 生成器是一个迭代器,迭代器不一定是生成器 # 迭代器满足两个条件:1、有iter方法,2、可以被next方法调用 l = [1, 2, 3, 5] d = iter(l) print(d) print( 阅读全文
posted @ 2024-09-14 15:38 GDquicksand 阅读(15) 评论(0) 推荐(0)
摘要:def consumer(name): print("%s 开始购买物品了!" % name) while True: item_name = yield print("物品 %s 被 %s 购买了!" % (item_name, name)) def producer(name): c = con 阅读全文
posted @ 2024-09-13 16:26 GDquicksand 阅读(5) 评论(0) 推荐(0)
摘要:# 把a创建成了一个生成器对象 generator object a = (x * 2 for x in range(10)) print(a) print(next(a)) # 生成器对象调用用next(a),等价于 a.__next__(),生成器一次调用一个 print(next(a)) fo 阅读全文
posted @ 2024-09-13 16:24 GDquicksand 阅读(14) 评论(0) 推荐(0)
摘要:# 获取文件内容 def open_text(login_text): with open(login_text, "r", encoding="UTF-8") as file: file = eval(file.read()) return file login_flag = False # 登录 阅读全文
posted @ 2024-09-12 15:58 GDquicksand 阅读(10) 评论(0) 推荐(0)
摘要: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 阅读全文
posted @ 2024-09-10 16:49 GDquicksand 阅读(12) 评论(0) 推荐(0)
摘要:# 局部变量不能修改全局变量,如需要修改是需要先进行声明 a = 1 # 全局变量 def f(): global a # 声明使用全局变量 print("第1次打印:%s" % a) a = 2 # 修改全局变量 print("第2次打印:%s" % a) def f_1(): global a 阅读全文
posted @ 2024-09-04 18:07 GDquicksand 阅读(9) 评论(0) 推荐(0)