[Tutor] unpickling a class

Andy W toodles@yifan.net
Sat, 22 Dec 2001 01:29:33 +0800


Hi Tim,

> I'm having trouble building a constructor method for the Portfolio class
> that allows me to *either* create a portfolio instance from scratch or
> load one from disk using the pickle module.

How about something like:

import pickle

class Portfolio:
  def __init__(self,pickle_file=None):
    if pickle_file is not None:
      pickled_instance=pickle.load(pickle_file)
      ... extract whatever you need from it
    else:
      ... continue as normal

I'm not sure if there's a way to make an instance become equal to another
value within the constructor, perhaps someone else can enlighten you (and me
at the same time! :)

HTH,
Andrew

>
> Any hints or do I need to post some code?
>
> -Tim
>
> --
> Tim Wilson      |   Visit Sibley online:   | Check out:
> Henry Sibley HS |  http://www.isd197.org   | http://www.zope.com
> W. St. Paul, MN |                          | http://slashdot.org
> wilson@visi.com |  <dtml-var pithy_quote>  | http://linux.com
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>