473,387 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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?

Anton
Aug 12 '06 #1
2 5562
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?

Anton
That won't work, but the following will. An object's attributes can be
accessed and set through the __dict__ attribute, and its class through
the __class__ attribute:

>>class A: # define class A
.... pass
....
>>a = A() # create an instance
a.__dict__ # It has no attributes
{}
>>a.__dict__ = {'c':1, 'b':2} # Give it some
a.b # Verify
2
>>a.c
1
>>a.__class__ # Examine its class
<class __main__.A at 0xb7da12fc>
>>class B:
.... pass
....
>>a.__class__ = B # Change its class
a.__class__ # Verify
<class __main__.B at 0xb7da129c>

Both the __dict__ and the __class__.__name__ could be pickled.

Gary Herron
Aug 12 '06 #2
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?

Anton
Why are you trying to do this? Usually you would just say obj =
pickle.load(f) and be done with it (you could manipulate obj after it's
created, of course.) Why do you want to do obj = Obj() but have the
obj come from "obj.dat"?

Just curious.

Peace,
~Simon

Aug 12 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jesse Bloom | last post by:
I keep running into a problem when I use pickle or cPickle to unpickle a python object I have serialized from the database. I read the string for the serialized object. Apparently I am missing an...
2
by: Jane Austine | last post by:
Hi. class A: def __init__(self,tick): if tick: self.foo=self.bar else: self.foo=self.bur def bar(self): print 'bar'
1
by: A.B., Khalid | last post by:
I wonder if someone can explain what is wrong here. I am pickling a list of dictionaries (see code attached) and unpickling it back using the HIGHEST_PROTOCOL of pickle and cPickle. I am getting an...
28
by: Grant Edwards | last post by:
I finally figured out why one of my apps sometimes fails under Win32 when it always works fine under Linux: Under Win32, the pickle module only works with a subset of floating point values. In...
9
by: peter.mosley | last post by:
Hello again I'm still wrestling with Python / Tkinter / PIL. I have almost got it to work - on a Fedora Core 1 system I can now display images in a Tkinter window. But I also run a Fedora Core...
0
by: greg.landrum | last post by:
Hi, I have a fairly sizable body of python code that I'm in the process of modernizing. One of the modernization steps is converting to use new-style classes, and this is leading to problems...
2
by: Steven Bethard | last post by:
I'd like to be able to pickle instancemethod objects mainly because I want to be able to delay a call like ``foo(spam, badger)`` by dumping ``foo``, ``spam`` and ``badger`` to disk and loading them...
2
by: Mario Ceresa | last post by:
Hello everybody: I'd like to use the pickle module to save the state of an object so to be able to restore it later. The problem is that it holds a list of other objects, say numbers, and if I...
2
blazedaces
by: blazedaces | last post by:
I'm designing a GUI for autonomous robot interaction, but that's not so important. To figure out the ins and outs of a certain aspect of PyQt I usually just do what I did when I learning Qt, I grab...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.