Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Treat error ?
#2
class Foo:
    def spam(self):
        pass

    
try:
    check = hasattr(Foo, 'eggs')
except AttributeError:
    check = False

print(check)
print(hasattr(Foo, 'spam'))


I was misled by your AttributeError...

you don't need try/except

class Foo:
    def spam(self):
        pass

check = hasattr(Foo, 'eggs')
print(check)
check = hasattr(Foo, 'spam')
print(check)
As a side note about using try/except - try should be before the line that may raise error, not after that.

try:
    print(1/0)
except ZeroDivisionError:
    print('Cannot divide by 0')
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
Treat error ? - by JohnnyCoffee - Apr-14-2020, 07:16 PM
RE: Treat error ? - by buran - Apr-14-2020, 07:49 PM
RE: Treat error ? - by JohnnyCoffee - Apr-14-2020, 11:39 PM
RE: Treat error ? - by buran - Apr-15-2020, 05:02 PM
RE: Treat error ? - by JohnnyCoffee - Apr-16-2020, 10:28 AM
RE: Treat error ? - by buran - Apr-16-2020, 11:40 AM
RE: Treat error ? - by JohnnyCoffee - Apr-16-2020, 11:23 PM
RE: Treat error ? - by buran - Apr-17-2020, 03:01 AM
RE: Treat error ? - by JohnnyCoffee - Apr-17-2020, 05:54 AM
RE: Treat error ? - by buran - Apr-17-2020, 07:30 AM
RE: Treat error ? - by JohnnyCoffee - Apr-17-2020, 07:35 AM
RE: Treat error ? - by buran - Apr-17-2020, 07:42 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to treat two token as one token(lexer)? hsunteik 1 3,613 Dec-28-2016, 12:26 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020