sziza
sziza

Reputation: 3

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);

Upvotes: 0

Views: 309

Answers (2)

Martijn Pieters
Martijn Pieters

Reputation: 1125398

The default representation for custom classes is to print their name and their 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).

Note that when printing a list or other container, the contents of that list will be shown by representations (repr() is used, not 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().

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 600059

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.

To fix this, add a __str__ method to the class:

class Person:
    ...
    def __str__(self):
        return 'This is {} and their email is {}'.format(self.name, self.email)

Upvotes: 1

Related Questions