[Tutor] for loops and exceptions

Matthew White mwhite3 at ttsd.k12.or.us
Wed Mar 29 20:56:31 CEST 2006


Hello,

>From a general style and/or programmatic perspective, which is a "better"
way to write this bit of code?

try:
    (dn, attrs) = conn.search_s(search_base, search_scope, search_filter, search_attrs):
except Exception, e:
        warn_the_user(e)
        do_something_useful()

for (name, data) in (dn, attrs):
        print '%s' % (name)
        for key, value in data.iteritems():
                print '%s => %s' % (key, value)

OR

try:
    for dn, attrs in conn.search_s(search_base, search_scope, search_filter, search_attrs):
        print dn
        for key, value in attrs.iteritems():
            print '\t%s => %s' % (key, value)
        print
except Exception, e:
     warn_the_user(e)
     do_something_useful


Personally I like the second method more.  To my eyes it is compact
and efficient.  Coming from a perl background I tend to like to get
very compact, which can be an impediment to code readability.  As I get
started with python I want to make sure that I don't repeat the same
mistakes and let too many perl-isms creep into my python code.

-mtw


-- 
Matthew White - District Systems Administrator
Tigard/Tualatin School District
503.431.4128

"The greatest thing in this world is not so much where we are, but in
what direction we are moving."   -Oliver Wendell Holmes



More information about the Tutor mailing list