/
eb
/
python_learn
Обзор
Документация
Войти
/
eb
/
python_learn
Код
Запросы
0
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
options/pickling.py
38 строк
959 B
EvgeniyBudaev
Pickle - консервирование
01 мар 2021, 05:19
01 мар 2021, 05:19
e2bb7c9
Код
Авторство
О чём код?
import pickle class Character(): def __init__(self, race, armor, damage = 10): self.race = race self.damage = damage self.armor = armor self.health = 100 def hit(self, damage): self.health -= damage def is_dead(self): return self.health == 0 # def __getstate__(self): def __setstate__(self, state): self.race = state.get('race', 'Elf') # Elf - значение по умолчанию self.damage = state.get('damage', 10) self.armor = state.get('armor', 20) self.health = state.get('health', 100) c = Character('Elf') c.hit(10) print(c.health) # 90 with open(r'C:\tmp\game_state.bin', 'w+b') as f: pickle.dump(c, f) # dump - преобразует объект в битовое представление c = None with open(r'C:\tmp\game_state.bin', 'rb') as f: c = pickle.load(f) print(c.health) # 90 print(c.__dict__) # {'race': 'Elf', 'damage': 10, 'armor': 20, 'health': 90}