473,805 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pickling and inheritance are making me hurt

I have a module that defines a Search class and a SearchResult class. I use
these classes by writing other modules that subclass both of them as needed
to interface with particular search engines.

My problem is that Search defines a method (called automatically by __del__)
to save its results between invocations:

def _saveresults(se lf):
self._oldresult s = self._results
file = open(self._stor efile(), 'w')
pickle.dump(sel f._oldresults, file)
file.close()

The problem I'm having is the the pickle.dump call is failing whenever the
objects in "self.data" are instances of derivatives of SearchResult rather
than instances of SearchResult itself (which is pretty much always the
case):

Exception pickle.Pickling Error: <pickle.Picklin gError instance at
0xb7f7ad6c> in <bound method Search.__del__ of <__main__.Searc h object at
0xb7ec954c>> ignored
Now, if I overload _saveresults inside a subclass of Search, then it works.
It seems like the problem is that _saveresults is only looking inside the
same namespace as Search (where it's originally defined), even if it's
actually been inherited by another class in a different module. Is there a
way around this?

--
Kirk Strauser
Jul 18 '05 #1
2 2258
Kirk Strauser <ki**@strauser. com> writes:
I have a module that defines a Search class and a SearchResult class. I use
these classes by writing other modules that subclass both of them as needed
to interface with particular search engines.

My problem is that Search defines a method (called automatically by __del__) ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^

Don't Do That. __del__ methods are bad. They mess up cyclic garbage
collection, IIRC. ISTR other problems with them, too...
[...] Exception pickle.Pickling Error: <pickle.Picklin gError instance at
0xb7f7ad6c> in <bound method Search.__del__ of <__main__.Searc h object at
0xb7ec954c>> ignored

[...]

....yeah, there's one: thanks for reminding me ;-)

You may want to read up on __getstate__ and __setstate__, BTW.
John
Jul 18 '05 #2
[Kirk Strauser]
I have a module that defines a Search class and a SearchResult class.
Try posting a minimal self-contained code sample that fails.
I use these classes by writing other modules that subclass both of them as
needed to interface with particular search engines.

My problem is that Search defines a method (called automatically by __del__)
to save its results between invocations:

def _saveresults(se lf):
self._oldresult s = self._results
file = open(self._stor efile(), 'w')
pickle.dump(sel f._oldresults, file)
file.close()

The problem I'm having is the the pickle.dump call is failing whenever the
objects in "self.data" are instances of derivatives of SearchResult rather
than instances of SearchResult itself (which is pretty much always the
case):

Exception pickle.Pickling Error: <pickle.Picklin gError instance at
0xb7f7ad6c> in <bound method Search.__del__ of <__main__.Searc h object at
0xb7ec954c>> ignored

Now, if I overload _saveresults inside a subclass of Search, then it works.
It seems like the problem is that _saveresults is only looking inside the
same namespace as Search (where it's originally defined), even if it's
actually been inherited by another class in a different module. Is there a
way around this?


Not enough relevant information to say. __del__ looks irrelevant
here, apart from that you happen to be pickling while __del__ is
executing. Try pickling an instance of a subclass of Search directly
to see what happens. There are many reasons for why PicklingError may
get raised.

It's certainly true that any function in Python (method or not)
executes with the globals of the module in which it's defined. While
you mentioned that as a possible issue, it seemed to come out of the
blue (didn't seem to follow from anything said before it).
Jul 18 '05 #3

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

Similar topics

1
2335
by: Marc | last post by:
Hi all, After some research I've decided that my previous question (Confusing problem between Tkinter.Intvar...) was headed in the wrong direction. Partly because I think I have a greater understanding of what was happening, and partly because pickling Tkinter widgets is an issue that seems to have been touched on over the years but never really acted on. Postings back to '96 have talked about the need to pickle Tk widgets.
1
2298
by: Edward Loper | last post by:
I'm having trouble pickling subclasses of dict when they contain cycles. In particular: >>> import pickle >>> class D(dict): pass >>> d = D() >>> d = d # add a cycle. >>> print d {1: {...}} >>> pickle.dump(d, open('d.pickle', 'w'))
2
1347
by: Daniel Bickett | last post by:
I was reading the "Pickling and inheritance are making me hurt" thread, and the latest suggestion (as of this posting) was to do with the __setstate__ and __getstate__ methods. They caught my attention because I hadn't encountered them before, and it reminded me that in the past I've never been able to very good, definitive documentation on newstyle classes. Googling for it gives little python tutorials on various sites, and even searching...
1
2282
by: Erik Max Francis | last post by:
I've come across a limitation in unpickling certain types of complex data structures which involve instances that override __hash__, and was wondering if it was known (basic searches didn't seem to come up with anything similar) and if there is a workaround for it short of restructuring the data structures in question. The fundamental issue rests with defining classes which override __cmp__ and __hash__ in order to be used as keys in...
9
3603
by: Alex | last post by:
I have a serious problem and I hope there is some solution. It is easier to illustrate with a simple code: >>> class Parent(object): __slots__= def __init__(self, a, b): self.A=a; self.B=b def __getstate__(self): return self.A, self.B def __setstate__(self, tup):
14
12927
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at all obvious in VBA which lacks inheritance. I'm trying the explanation again now. I often find cases where a limited form of inheritance would eliminate duplication my code that seems impossible to eliminate otherwise. I'm getting very...
3
3615
by: sushant.sirsikar | last post by:
Hi, I am new in Python World.I want to know what is mean by ``pickling'' and ``unpickling'' ? And how can we used it?Please Give Me some links of Picking Examples. Thanks Sushant
0
971
by: George Sakkis | last post by:
I stumbled on some weird pickling behavior that after some non-trivial debugging time turned out to be caused by __getstate__ returning an empty dict for some instances. As I found digging through the docs (http://pythondoc.kldp.net/lib/pickle-inst.html), surprisingly __setstate__ is not called nor new style instances if __getstate__ returns a false valse, without any further justification. Any pointers on the rationale for making a...
7
3743
by: Adam Nielsen | last post by:
Hi everyone, I'm having some trouble getting the correct chain of constructors to be called when creating an object at the bottom of a hierarchy. Have a look at the code below - the inheritance goes like this: Shape | +-- Ellipse | +-- Circle
0
9716
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10607
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10359
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10364
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6875
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3843
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.