Last active
August 29, 2015 14:25
-
-
Save Daetalus/ec5034f08f597fee00de to your computer and use it in GitHub Desktop.
Inconsistent error message in Python?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# New style class | |
class B(object): | |
def __init__(self, n): | |
self._n = n | |
c = B(3) | |
d = B(4) | |
try: | |
print(pow(c, d)) | |
except Exception as e: | |
print type(e), e.message | |
try: | |
print(pow(c, d, d)) | |
except Exception as e: | |
print type(e), e.message | |
# will out put: | |
# <type 'exceptions.TypeError'> unsupported operand type(s) for ** or pow(): 'B' and 'B' | |
# <type 'exceptions.TypeError'> unsupported operand type(s) for pow(): 'B', 'B', 'B' | |
# Change it to old style class | |
class B: | |
def __init__(self, n): | |
self._n = n | |
c = B(3) | |
d = B(4) | |
try: | |
print(pow(c, d)) | |
except Exception as e: | |
print type(e), e.message | |
try: | |
print(pow(c, d, d)) | |
except Exception as e: | |
print type(e), e.message | |
# will out put: | |
# <type 'exceptions.TypeError'> unsupported operand type(s) for ** or pow(): 'instance' and 'instance' | |
# <type 'exceptions.AttributeError'> B instance has no attribute '__pow__' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment