Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
exception due to NoneType
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
asit  
View profile  
 More options Nov 8, 5:06 am
Newsgroups: comp.lang.python
From: asit <lipu...@gmail.com>
Date: Sat, 7 Nov 2009 10:06:43 -0800 (PST)
Local: Sun, Nov 8 2009 5:06 am
Subject: exception due to NoneType
In my program I want to catch exception which is caused by accessing
NoneType object.

Can anyone suggest me how this can be done ??


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bruno Desthuilliers  
View profile  
 More options Nov 8, 4:36 am
Newsgroups: comp.lang.python
From: Bruno Desthuilliers <bdesth.quelquech...@free.quelquepart.fr>
Date: Sat, 07 Nov 2009 18:36:08 +0100
Local: Sun, Nov 8 2009 4:36 am
Subject: Re: exception due to NoneType
asit a écrit :

> In my program I want to catch exception which is caused by accessing
> NoneType object.

> Can anyone suggest me how this can be done ??

Not without the minimal working code exposing your problem, or the full
traceback you got. Merely "accessing NoneType object" doesn't by itself
raise any exception... I suspect you get the None object where you
expected something else and try to access an attribute of this
'something else', and ends up getting an AttributeError, but there are
other possible scenarii that might fit your (very poor) description of
the problem, so no way too help you without more informations. As a
general rule, remember that the traceback is actually meant to *help*
finding out what went wring.

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
asit  
View profile  
 More options Nov 8, 6:08 am
Newsgroups: comp.lang.python
From: asit <lipu...@gmail.com>
Date: Sat, 7 Nov 2009 11:08:18 -0800 (PST)
Local: Sun, Nov 8 2009 6:08 am
Subject: Re: exception due to NoneType
On Nov 7, 10:36 pm, Bruno Desthuilliers

I could have described the error, but the problem is that it's
dependent of a third party library..

Let me write the code here...

import twitter

api = twitter.Api('asitdhal','swordfish')
users = api.GetFriends()
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

look at the except TypeError. This is supposed to catch only exception
thrown by NoneType.

please help me.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Diez B. Roggisch  
View profile  
 More options Nov 8, 9:16 am
Newsgroups: comp.lang.python
From: "Diez B. Roggisch" <de...@nospam.web.de>
Date: Sat, 07 Nov 2009 23:16:28 +0100
Local: Sun, Nov 8 2009 9:16 am
Subject: Re: exception due to NoneType

> api = twitter.Api('asitdhal','swordfish')

You just gave the world the account-information to your twitter-account.
  You'd rather change these asap, or somebody hijacks your account...

Diez


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bruno Desthuilliers  
View profile  
 More options Nov 8, 9:02 am
Newsgroups: comp.lang.python
From: Bruno Desthuilliers <bdesth.quelquech...@free.quelquepart.fr>
Date: Sat, 07 Nov 2009 23:02:53 +0100
Local: Sun, Nov 8 2009 9:02 am
Subject: Re: exception due to NoneType
asit a écrit :

This more often than not translates to "dependent of a wrong use of a
3rd part library".

> Let me write the code here...

> import twitter

> api = twitter.Api('asitdhal','swordfish')

I hope this is not your actual login.

> users = api.GetFriends()
> for s in users:
>     print
>     print "##########################################"
>     try:
>         print "user id : " + str(s.id)

Please learn to use print and string formatting. Here are two ways to
get rid of the need to use str():

1/          print "user id : %s" % s.id
2/          print "user id : ", 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

Silently passing exception is 99.999 out of 100 a very stupid thing to do.

> look at the except TypeError.

Yes, I've seen it. It's stupid, and actually make debugging harder. Any
statement in this try block could raise a TypeError if the looked up
attribute of 's' is not a string. Python is NOT php, and will not
happily adds apples and parrots - you can only concatenate strings with
strings, not with ints or None or whatever...

> This is supposed to catch only exception
> thrown by NoneType.

Please get your facts straight.
1/ The NoneType doesn't "throw" (the appropriate python term is "raise")
 any exception by itself
2/ this except clause will catch any TypeError. Try this:

try:
   x = "aaa" + 42
except TypeError, e:
   print "This has nothing to do with NoneType"

> please help me.

Help yourself and read the FineManual. Then make appropriate use of
string formatting (your best friend for simple formatted outputs)
instead of useless string concatenations.

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Finney  
View profile  
 More options Nov 8, 10:14 am
Newsgroups: comp.lang.python
From: Ben Finney <ben+pyt...@benfinney.id.au>
Date: Sun, 08 Nov 2009 10:14:31 +1100
Local: Sun, Nov 8 2009 10:14 am
Subject: Re: exception due to NoneType

asit <lipu...@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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google