On 22/12/15 00:17, jamie hu wrote: > I think storing them as a list in memory solves this issue. If I were to > use database I will need to search and create object again while returning > to user. Is that right?*
Yes, although you can keep a list of instantiated objects in memory (sometimes referred to as an object pool or cache). What you should not do with database is simply instantiate every object in the database and keep it in memory for the lifetime of the app. You can either instantiate then delete as you go or sometimes have a policy of using a fixed number of objects in memory and delete on a least recently used basis. This is more work but can yield performance improvements where a relatively few objects get repeatedly accessed. When you need to instantiate an object check the pool to see if its already there, and if not such it out of the database. You can hide most of that inside a factory function or class method: def getFoo(ID): try: return Foo._instances[ID] except KeyError: return Foo(ID) # init() accesses DB HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist - [email protected] To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
