摘要: 阅读全文
posted @ 2020-06-12 23:50 abel2020 阅读(118) 评论(0) 推荐(0) 编辑
摘要: # #打开文件 file1 = open("test2.py") #默认只读 file2=open("test2.py.copy","w") # w 覆盖写, a 追加,没有文件都会创建 #读取文件 text = file1.read() #写入文件 file2.write(text) #关闭文件 阅读全文
posted @ 2020-06-12 23:43 abel2020 阅读(148) 评论(0) 推荐(0) 编辑
摘要: #1 打开文件,open 函数 file1 = open("test2.py") #2读取文件,read text = file1.read() #返回字符串类型 print(text) #3 关闭文件 file1.close() # file1 = open("test2.py") text = 阅读全文
posted @ 2020-06-12 22:59 abel2020 阅读(118) 评论(0) 推荐(0) 编辑
摘要: 1 新建 python Package : mess 文件夹,自动生成空的 __init__.py ,创建send_mess ,receive_mess 两个测试文件, __init__.py 如下: from . import receive_mess from . import send_mes 阅读全文
posted @ 2020-06-12 18:54 abel2020 阅读(99) 评论(0) 推荐(0) 编辑
摘要: #导入模块 #定义全局变量 #定义类 #定义函数 def main(): pass # ... print("测试本py代码") if __name__ == '__main__': # 只有在本次程序中,两个才会相等,下面才会执行 main() 阅读全文
posted @ 2020-06-12 18:31 abel2020 阅读(83) 评论(0) 推荐(0) 编辑
摘要: def input_passwd(): pwd=input("请输入密码:") if len(pwd) >=8: return pwd else: # 1 创建异常对象 使用错误信息字符串作为参数:如 密码长度不够 ex=Exception("密码长度不够") #Exception 是py的异常对象 阅读全文
posted @ 2020-06-12 17:23 abel2020 阅读(118) 评论(0) 推荐(0) 编辑
摘要: try: #可能出现错误的代码 num=input(("please input number:")) d except error_code1: # 针对错误类型(最后一行第一个单词),执行如下代码 pass except Exception as err_other: print(err_oth 阅读全文
posted @ 2020-06-12 16:48 abel2020 阅读(109) 评论(0) 推荐(0) 编辑
摘要: """类中只有一个对象1 重写new方法 只初始化一次内存2 设置标志位,使init 在多次被执行时,实际代码只被执行一次""" class MusicPlayer(object): instance=None #初始没有对象, 定义类的instance属性,设为空 # new是object类内置的 阅读全文
posted @ 2020-06-12 10:03 abel2020 阅读(113) 评论(0) 推荐(0) 编辑
摘要: class Game: top_score=0 #定义类属性 @classmethod #定义类方法,因为要调用类属性 def show_top_score(cls): print("top score :%s "% cls.top_score) @staticmethod #定义静态方法,什么都不 阅读全文
posted @ 2020-06-12 00:57 abel2020 阅读(72) 评论(0) 推荐(0) 编辑
摘要: class Animal(object): @staticmethod #定义静态方法,eat 不要调用类的属性,也不要调用对象的属性 def eat(): print("eat") # 静态方法,不需要创建实例对象 Animal.eat() 阅读全文
posted @ 2020-06-12 00:03 abel2020 阅读(67) 评论(0) 推荐(0) 编辑