Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: self=pickle.load(file)? (Object loads itself)

Reply
Thread Tools

Re: self=pickle.load(file)? (Object loads itself)

 
 
Jean-Paul Calderone
Guest
Posts: n/a
 
      08-12-2006
On Sat, 12 Aug 2006 18:36:32 +0200, Anton81 <> wrote:
>Hi!
>
>it seems that
>
>class Obj:
> def __init__(self):
> f=file("obj.dat")
> self=pickle.load(f)
>...
>
>doesn't work. Can an object load itself with pickle from a file somehow?
>What's an easy solution?


You are trying to implement a constructor (__new__) for the Obj class, but you have actually implemented the initializer (__init__). In order to be able to control the actual creation of the instance object, you cannot use the initializer, since its purpose is to set up various state on an already created instance. Instead, you may want to use a class method:

class Obj:
def fromPickleFile(cls, fileName):
return pickle.load(file(fileName))
fromPickleFile = classmethod(fromPickleFile)

You can then use this like so:

inst = Obj.fromPickleFile('obj.dat')

Jean-Paul


>
>Anton
>--
>http://mail.python.org/mailman/listinfo/python-list
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wireless Network card driver only loads under Admin account =?Utf-8?B?TmVpbCBQ?= Wireless Networking 1 02-22-2006 10:02 AM
Missing icons on splash screen for installed products when IDE loads CodeMonkey ASP .Net 0 10-11-2005 02:28 PM
Loads Slowly Mooreslaw Firefox 3 01-28-2005 05:55 AM
Old file loads in Mozilla, New file in IExplorer Tommy DN Firefox 1 07-04-2004 09:33 AM
Re: Including javascripts in HTML - it appears as the script never loads Bob Walton Perl 0 07-19-2003 02:30 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57