Python Cookbook
About This Book
Portable, powerful, and a breeze to use, Python is the popular open source object-oriented programming language used for both standalone programs and scripting applications. It is now being used by an increasing number of major organizations, including NASA and Google.Updated for Python 2.4, The Python Cookbook, 2nd Edition offers a wealth of useful code for all Python programmers, not just advanced practitioners. Like its predecessor, the new edition provides solutions to problems that Python programmers face everyday.It now includes over 200 recipes that range from simple tasks, such as working with dictionaries and list comprehensions, to complex tasks, such as monitoring a network and building a templating system. This revised version also includes new chapters on topics such as time, money, and metaprogramming.
Here's a list of additional topics covered:
- Manipulating text
- Searching and sorting
- Working with files and the filesystem
- Object-oriented programming
- Dealing with threads and processes
- System administration
- Interacting with databases
- Creating user interfaces
- Network and web programming
- Processing XML
- Distributed programming
- Debugging and testing
Another advantage of The Python Cookbook, 2nd Edition is its trio of authors--three well-known Python programming experts, who are highly visible on email lists and in newsgroups, and speak often at Python conferences.With scores of practical examples and pertinent background information, The Python Cookbook, 2nd Edition is the one source you need if you're looking to build efficient, flexible, scalable, and well-integrated systems.
Here is the example in Alex Martelli Python Cookbook that show how to create a memoize decorator using cPickle for function that take mutable argument (original version) :
Here is a link.
EDIT: Using the code that you have given and this memoize decorator :
i was able to reproduce this:
Programming language teaching has always been associated with a cliche statement while learning. "Write programs to learn programming". I too would suggest the same.
If you are going to start from basics. This is of course, the most suggested starting point. It is lengthy, but it is worth all the time. http://www.diveintopython.org/
Because you are into some Java, this might be even better for you. http://www.swaroopch.com/notes/Python. Start either python 2.x or 3.0. Me personally am a fan of python 3. But for a starter it could be hard to get samples, and references to programs online. So for you 2.x might be better. But I leave it upto you.
Like I started "Write programs..". You can start here.
After you think you have gained sufficient proficiency in python, you can try recipes in this book python cookbook http://www.amazon.com/Python-Cookbook-Alex-Martelli/dp/0596007973.
For application development, after you think you can handle it, start on wxPython or PyQt. I personally would suggest PyQt. It is responsive, fast, and has decent development cycle, I have not used WxPython for long, but few programs I wrote, long back, didn't feel so great. Yet again, its upto you.
Have you read the Python Cookbook? It's a pretty good source for Pythonic.
Plus you'll find much more from Alex Martelli on Stack Overflow.
+1 for Dive into Python and Python in a Nutshell. I also highly recommend effbot's Guide to the Standard Library. You'll probably also want to check out the Python Cookbook for some good examples of idiomatic Python code. Check out Foundations of Python Networking to pick up where the SysAdmin book leaves off in terms of network protocols (fyi: all APress books are available as PDFs, which I love)
What you are looking for is Denis Otkidach's excellent CachedAttribute:
It can be used like this:
Notice that accessing
foo.bar
subsequent times does not call the getter function. (Calculating self.bar
is not printed.)Deleting
foo.bar
fromfoo.__dict__
re-exposes the property defined inFoo
. Thus, callingfoo.bar
again recalculates the value again.The decorator was published in the Python Cookbook and can also be found on ActiveState.
This is efficient because although the property exists in the class's
__dict__
, after computation, an attribute of the same name is created in the instance's__dict__
. Python's attribute lookup rules gives precedence to the attribute in the instance's__dict__
, so the property in class becomes effectively overridden.