exception due to NoneType

Ben Finney ben+python at benfinney.id.au
Sun Nov 8 00:14:31 CET 2009


asit <lipun4u at gmail.com> writes:

> for s in users:
>     print
>     print "##########################################"
>     try:
>         print "user id : " + str(s.id)
>         print "user name : " + s.name
>         print "user location : " + s.location
>         print "user description : " + s.description
>         print "user profile image url : " + s.profile_image_url
>         print "user url : " + s.url
>         print "user status : " + str(s.status)
>     except TypeError:
>         pass

Why are you catching TypeError, only to discard it? Ignoring an error
doesn't make it go away.

The above code should rather use string formatting to interpolate the
values you want into a string for output::

    import textwrap
    
    for user in users:
        print textwrap.dedent("""
            ##########################################
            user id: %(id)s
            user name: %(name)s
            user location: %(location)s
            user description: %(description)s
            user profile image url: %(profile_image_url)s
            user url: %(url)s
            user status: %(status)s
            """) % vars(user)

-- 
 \       “My classmates would copulate with anything that moved, but I |
  `\               never saw any reason to limit myself.” —Emo Philips |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list