[Baypiggies] Discussion for newbies/beginner night talks

Shannon -jj Behrens jjinux at gmail.com
Sat Feb 10 05:39:40 CET 2007


#) Don't write complicated classes that have similar APIs to dicts,
lists, etc.  If it looks like a dict and smells like a dict, use a
dict.  Don't write a bunch of boilerplate API.  You can either
subclass a real dict, subclass UserDict (in rare situations), mixin
the UserDictMixin (spell? also in rare situations), etc., or just
provide access to the underlying dict.

#) Don't write getters and setters that don't actually do anything
except set a private instance member.  If that's the case, just use a
public instance member.  Later, you can switch to a real property
using the idiom in the Python cookbook
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/205183:

def foo():
        doc = "property foo's doc string"
        def fget(self):
            return self._foo
        def fset(self, value):
            self._foo = value
        def fdel(self):
            del self._foo
        return locals()  # credit: David Niergarth
    foo = property(**foo())

#) Need something?  Use the online Python Cookbook ;)  Got something
cool?  Submit it to the cookbook.  The code there isn't always
perfect, but it's usually a really good starting point!

Happy Hacking!
-jj

-- 
http://jjinux.blogspot.com/


More information about the Baypiggies mailing list