本文共 796 字,大约阅读时间需要 2 分钟。
(属性不可修改用_修饰)
class Player(): def __init__(self, name, hp, occr): self.name = name self.hp = hp self.__occr = occr def print_Method(self): print("name is", self.name, " hp is", self.hp," occr is", self.__occr)user2 = Player('tom', 100)user3 = Player('jerry', 80)user2.print_Method()user3.print_Method()
with open('name.txt') as f: print(f.read())
自定义with的方法
class TestWith(): def __enter__(self): print("start") def __exit__(self, exc_type, exc_val, exc_tb): if exc_tb is None: print("stop") else: print('has error',exc_tb) # 使用with TestWith(): print('to do something') raise NameError('testNameError')
输出
start to do something has error <traceback object at 0x1045b8988>
转载地址:http://kkesi.baihongyu.com/