473,377 Members | 1,151 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,377 software developers and data experts.

Pickle, __init__, and classes

Yin
I've created a class that reads in and processes a file in the
initializer __init__. The processing is fairly substantial, and thus,
instead of processing the file every time the object is created, I
pickle the object to a file.

In subsequent creations of the object, I implement a test to see
whether the pickled file exists. If it does, then I unpickle the
object.

Unfortunately, the __init__ cannot return this unpickled object.

I've used an auxiliary member function which fixes the problem, but I
would prefer to have the correct object returned at instantiation
rather than returned from the auxiliary member function.

Are there other ways of approaching this problem?

Thanks,
Yin

Aug 2 '05 #1
2 1396
"Yin" <yi*******@yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
I've created a class that reads in and processes a file in the
initializer __init__. The processing is fairly substantial, and thus,
instead of processing the file every time the object is created, I
pickle the object to a file.

In subsequent creations of the object, I implement a test to see
whether the pickled file exists. If it does, then I unpickle the
object.

Unfortunately, the __init__ cannot return this unpickled object.


Try __new__(). http://docs.python.org/ref/customization.html This isn't
the usual application of __new__, but since it returns an object it should
be ideal for your purposes.


Aug 2 '05 #2
Russell Blau wrote:
Try __new__(). http://docs.python.org/ref/customization.html This isn't
the usual application of __new__, but since it returns an object it should
be ideal for your purposes.


Or simply create a factory function to return your instances -- which is
in effect what __new__ does -- but without the baggage.

-Peter
Aug 2 '05 #3

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

Similar topics

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'
3
by: Michael Hohn | last post by:
Hi, under python 2.2, the pickle/unpickle sequence incorrectly restores a larger data structure I have. Under Python 2.3, these structures now give an explicit exception from...
3
by: Philippe C. Martin | last post by:
Hi, Shuffling files around in my project, I notice I broke everything when I stopped declaring classes in a program that "pickled.loaded" existing pickled object of type "classes". The error...
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...
3
by: dgdev | last post by:
I would like to pickle an extension type (written in pyrex). I have it working thus far by defining three methods: class C: # for pickling __getstate__(self): ... # make 'state_obj' return...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.