pickle 的dump的一点问题
各位大神:关于下面这段程序室第46讲的作业,但是我一执行,test.x = 123, 就报错: 我看了下 word,pickle_file , 都没啥问题,,所以是哪出错了啊。。
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
test.y = 123
File "C:\Users\zhuzj\Desktop\python\test\老教材\test.py", line 20, in __set__
self.record()
File "C:\Users\zhuzj\Desktop\python\test\老教材\test.py", line 33, in record
p.dump( self.words, self.pickle_file)
AttributeError: type object 'Path' has no attribute 'dump'
import pickle
from pathlib import Path as p
import time as t
class MyDes:
##初始化键值对,对应的变量 与值利用字典的属性链接起来,初始值为None
def __init__(self, name):
self.name = name
self.dictt = {}
self.dictt = None
def __get__(self, inst, owner):
self.time = t.ctime()
self.record()
return self.dictt
def __set__(self, inst, value):
self.time = t.ctime()
self.dictt = value
self.record()
def record(self):
self.path = r'C:\Users\zhuzj\Desktop\python\test\老教材' + f'\{self.name}.pkl'
self.words = f'{self.name} 变量于北京时间 {self.time}被读取, {self.name} = {self.dictt}\n'
with open(self.path , 'wb') as self.pickle_file:
p.dump(self.words, self.pickle_file)
class Test:
x = MyDes('x')
y = MyDes('y')
test = Test()
'''
0. 将拥有特殊方法(包括get, set ,delete)的类的实例 定义给其他类的属性,这个属性称之为描述符
1. __get__ (self, inst ,owner), __set__ (self , inst , value ), __delete__ (self, inst)
2. test.a
3. 0 , 先init , set , get
4. 9
'''
#动动手2
#动动手1
from pathlib import Path as p
import time as t
class Record:
#初始化键值对,对应的变量 与值利用字典的属性链接起来
def __init__(self, value = 0 , name = ''):
self.dictt = {}
self.name = name
self.dictt = value
#访问属性
def __get__(self, inst , owner):
self.time = t.ctime()
self.recordFile()
return self.dictt
#更改属性值
def __set__(self, inst , value):
self.time = t.ctime()
self.dictt = value
self.recordFile()
#生成保存日志
def recordFile(self):
with open(r'C:/Users/zhuzj/Desktop/python/test/老教材/record.txt','a+') as self.file:
self.words = f' {self.name} 变量于北京时间 {self.time}被读取, {self.name} = {self.dictt}\n '
self.file.write(self.words)
class Test:
x = Record(10, 'x')
y = Record(8.8, 'y')
test = Test()
test.x
test.y
test.x = 123
test.x = 1.23
test.y = 'i love fishc'
#动动手0
class MyDes:
def __init__(self, value = 0 , name = ''):
self.dictt = {}
self.value = value
self.name = name
self.dictt = self.value
def __get__(self, inst , owner):
print(f'正在获取变量:{self.name}')
return self.dictt
def __set__(self, inst , value):
print(f'正在修改变量:{self.name}')
self.dictt = value
def __delete__(self , inst):
print(f'正在删除变量:{self.name}\n哦这个变量没法删除 ')
class Test:
x = MyDes(10 , 'x')
test = Test()
test.x
import pickle
from pathlib import Path as p
import time as t
class MyDes:
##初始化键值对,对应的变量 与值利用字典的属性链接起来,初始值为None
def __init__(self, name):
self.name = name
self.dictt = {}
self.dictt = None
def __get__(self, inst, owner):
self.time = t.ctime()
self.record()
return self.dictt
def __set__(self, inst, value):
self.time = t.ctime()
self.dictt = value
self.record()
def record(self):
self.path = r'C:\Users\zhuzj\Desktop\python\test\老教材' + f'\{self.name}.pkl'
self.words = f'{self.name} 变量于北京时间 {self.time}被读取, {self.name} = {self.dictt}\n'
with open(self.path , 'wb') as self.pickle_file:
pickle.dump(self.words, self.pickle_file) # 这里改成 pickle
class Test:
x = MyDes('x')
y = MyDes('y')
test = Test()
test.x = 123 isdkz 发表于 2022-4-22 20:10
自己好似一个傻子。。。
页:
[1]