how to store instance variables of a class in a file using pickle

Question

I wanted to create a class item to store information of different items, existing in a shop. At least following is to be stored w.r.t. each item code, name, price, qty. Write a program to accept the data from user and store it permanently in the file.

Here's what i did:

import pickle
class item:
    def __init__(self,code,name,price,qty):
        self.code=code
        self.price=price
        self.name=name
        self.qty=qty
    def store(self):
        p=open('kk.dat','wb+')
        pickle.dump(self,p)

    def show(self):
        p=open('kk.dat','rb')
        x=pickle.load(p)
        print x
        p.close()
o = item(1121,'we',340,2)
o.store()
o.show()

Show source
| class   | python   | file-handling   | pickle   2017-01-06 10:01 0 Answers

Answers to how to store instance variables of a class in a file using pickle ( 0 )

Leave a reply to - how to store instance variables of a class in a file using pickle

◀ Go back