[英]Python pickle prints memory address?
I wrote one application for phone and email addresses but when I want to print the data I got memory address instead of the data. 我为电话和电子邮件地址编写了一个应用程序,但是当我要打印数据时,我得到的是内存地址而不是数据。
The code is following: 代码如下:
import pickle
class Person:
def __init__(self, name, lastName, phone, email):
self.name=name;
self.lastName=lastName;
self.phone=phone;
self.email=email;
class Adressar:
def __init__(self,):
print('telefonski imenik');
def interface(self):
print('Upišite broj od 1 do 4 za izbor funkcije');#write No from 1 to 4 for function selection
print('Upiši 1 za upis novog kontakta');#No 1 for adding new conntact
print('Upiši 2 za pretragu kontakta');#No 2 for contact search
print('Upiši 3 za brisanje kontakta');#No 3 for contact erasing
print('Upiši 4 za izlistavanje svih kontakata');#No 4 for giving list of all contacts
num=int(input('Upis odgovarajućeg broja... '));#input number
if num>4:
print('Greška! Upisi broj od 1 do 4!');#no is larger than 4
else:
return num;
def addPerson(self):
with open("adresar.pickle", 'wb') as fileIn:
name=input('Upiši ime: ');#write name
lastName=input('Upiši prezime: ');#write last name
phone=input('Upiši broj telefona: ');#write phone no
email=input('Upiši email: ');#write email
pickle.dump(Person(name, lastName, phone, email), fileIn);
start=Adressar();
num=start.interface();
storedList=[];
if num==1:
start.addPerson();
with open("adresar.pickle", 'rb') as f:
storedList=pickle.load(f);
print(storedList);
This isn't anything to do with pickle; 这和泡菜无关。 if you just created a Person directly and printed it you would get the same result.
如果您只是直接创建一个人并打印出来,您将得到相同的结果。 That's because you haven't told Python how it should print that type of object, and so it uses the default which is the memory address.
那是因为您没有告诉Python如何打印该类型的对象,因此它使用默认值即内存地址。
To fix this, add a __str__
method to the class: 要解决此问题,请在类中添加
__str__
方法:
class Person:
...
def __str__(self):
return 'This is {} and their email is {}'.format(self.name, self.email)
The default representation for custom classes is to print their name and their id()
: 自定义类的默认表示是打印其名称和
id()
:
>>> class Foo: pass
...
>>> Foo()
<__main__.Foo instance at 0x106aeab90>
You can give a class a __repr__
method to change the default representation, or a __str__
method to control how instances are converted to strings (such as by the print
function). 您可以为类提供
__repr__
方法来更改默认表示形式,或者为__str__
方法提供控制实例如何转换为字符串的方式(例如通过print
函数)。
Note that when printing a list or other container, the contents of that list will be shown by representations ( repr()
is used, not str()
). 请注意,在打印列表或其他容器时,该列表的内容将由表示形式显示(使用
repr()
,而不是str()
)。
Note that in your code, you only ever load the first person in the file; 请注意,在您的代码中,您只能加载文件中的第一个人。 you never store a list of instances, you instead replace the contents of the file with a new
pickle
every time you call addPerson()
. 您从不存储实例列表,而是在每次调用
addPerson()
时用新的pickle
替换文件的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.