0

I learn object-oriented programming in python 3. I've some exception, for example:

try:
    self.result = (...)
except urllib.error.URLError as error:
    print(error)

Generally all variables in classes are prefixed with self. Adding self before error variable:

try:
    self.result = (...)
except urllib.error.URLError as self.error:
    print(self.error)

causes:

SyntaxError: invalid syntax

Shall I just skip self before variables containing reason of exception?

1
  • 8
    'Generally all variables in classes are prefixed with self.' is a comment that shows a massive lack of understanding - you should read up on what self is before trying to use it - understanding the concepts will make everything a lot easier, and it's not a hard thing once you understand it. Commented Oct 6, 2012 at 14:30

1 Answer 1

4

error is the name you gave the exception. It is not a member of your class and thus is not prefixed with self.

Sign up to request clarification or add additional context in comments.

1 Comment

Interestingly enough, the documented grammar for the try compound statement doesn't disallow using self.error as a target (the target token includes attribute references). However, because the target is to be cleared when the except suite completes, only a local identifier is allowed, really. This is probably a bug in the documentation..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.