Tags: 4couple, __init__, a7self, b1231, class, disk, etci, foodef, lists, members, object, programming, python, sayclass, self, tuples, writing

Writing Object Data to Disk

On Programmer » Python

5,331 words with 9 Comments; publish: Mon, 28 Apr 2008 05:51:00 GMT; (200156.25, « »)

Hello,

I have a Python class with data members, say:

class Foo:

def __init__(self):

self.a=7

self.b[]={1,3,4}

#couple of more lists, tuples, etc

I would like to know if "Pickling" the class object is the only way of

writing it to disk for persistent storage. Also, do we have a concept

similar to "array of objects" in Python? The number of objects is only

known at "run-time".

Thanks,

Amit Kumar Saha

me blogs.python.todaysummary.com. http://amitksaha.blogspot.com

URL:http://amitsaha.in.googlepages.com

All Comments

Leave a comment...

  • 9 Comments
    • In message <mailman.914.1190444082.2658.python-list.python.todaysummary.com.python.org>, Amit Kumar

      Saha wrote:

      > I would like to know if "Pickling" the class object is the only way of

      > writing it to disk for persistent storage.

      Wouldn't it be better to use a language-independent data representation that

      wasn't tied to Python? Also tying your external data representation to your

      internal program structure (objects) makes it harder to change the program

      in future.

      #1; Mon, 28 Apr 2008 05:53:00 GMT
    • "Amit Kumar Saha" <da...o.in> wrote:

      > I would like to know if "Pickling" the class object is the only way of

      > writing it to disk for persistent storage.

      look at marshall and shelve

      >.............................................Also, do we have a[/colo

      r]

      concept

      > similar to "array of objects" in Python? The number of objects is only

      > known at "run-time".

      I would use a list

      - Hendrik

      #2; Mon, 28 Apr 2008 05:54:00 GMT
    • Amit Kumar Saha wrote:

      > I have a Python class with data members, say:

      > class Foo:

      > def __init__(self):

      > self.a=7

      > self.b[]={1,3,4}

      ^^

      This is invalid syntax.

      > I would like to know if "Pickling" the class object is the only

      > way of writing it to disk for persistent storage.

      It isn't. Every means of serialisation works, but pickling is

      especially convenient if you only use Python.

      > Also, do we have a concept similar to "array of objects" in

      > Python?

      You can put all objects in lists, be it a string, an integer or some

      complex class instance.

      > The number of objects is only known at "run-time".

      class and function definitions also happen at runtime. I suggest you

      worked through the Python tutorial to get a deeper insight in

      Python's concepts.

      http://docs.python.org/tut/

      Regards,

      Bjrn

      BOFH excuse #89:

      Electromagnetic energy loss

      #3; Mon, 28 Apr 2008 05:55:00 GMT
    • On 22/09/2007 7:16 PM, Bjoern Schliessmann wrote:

      > Amit Kumar Saha wrote:

      > ^^

      > This is invalid syntax.

      So are the braces.

      #4; Mon, 28 Apr 2008 05:56:00 GMT
    • On Sat, 22 Sep 2007 17:13:14 +0530, Amit Kumar Saha wrote:

      > BTW, do we have something like array of objects here?

      Like someone already said: lists.

      Ciao,

      Marc 'BlackJack' Rintsch

      #5; Mon, 28 Apr 2008 05:57:00 GMT
    • "Lawrence D'Oliveiro" <ldo.python.todaysummary.com.g-central.gen.new_zealand> wrote in message

      news:fd300j$ag$1.python.todaysummary.com.lust.ihug.co.nz...

      > In message <mailman.922.1190461411.2658.python-list.python.todaysummary.com.python.org>, Amit

      > Kumar

      > Saha wrote:

      >

      > Even so, ignoring the issue of future changes is folly.

      Can't Python tie into a DB such as MySQL?

      I'm a C++ programmer currently, but have programmed in many many languages,

      I only use Python to help someone work on an internet service and to modify

      some Python code for OpenRPG, so I'm not real familiar with all Python can

      actually do.

      #6; Mon, 28 Apr 2008 05:58:00 GMT
    • > I would like to know if "Pickling" the class object is the only way of

      > writing it to disk for persistent storage. Also, do we have a concept

      > similar to "array of objects" in Python? The number of objects is only

      > known at "run-time".

      Have a look at YAML.

      http://freshmeat.net/projects/syck/

      Also it is possible to persist python data to and from XML. See

      xml.marshal.generic.dumps

      #7; Mon, 28 Apr 2008 05:59:00 GMT
    • Amit Kumar Saha wrote:

      > Actually, language independence is really not a consideration here. I am

      > happy at having it tied to Python :-)

      > BTW, do we have something like array of objects here?

      In numpy, one can have a multi-dimensional array of objects. It would

      be good to have this notion implemented in Python itself.

      Colin W.

      #8; Mon, 28 Apr 2008 06:00:00 GMT
    • David a crit :

      > Have a look at YAML.

      > http://freshmeat.net/projects/syck/

      > Also it is possible to persist python data to and from XML. See

      > xml.marshal.generic.dumps

      or JSON...

      #9; Mon, 28 Apr 2008 06:01:00 GMT