### Thread: Exception raising

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2013
Posts
10
Rep Power
0

#### Exception raising

i have a better question this time and while i'm sure it's going to be something obvious, to me it is not so obvious. i'll print the code and ask my question.

Code:
```class MuffledCalculator:
muffled = 0
def calc(self, expr):
try:
return eval(expr)
except ZeroDivisionError:
if
self.muffled: print 'Division by zero is illegal'
else:
raise```
This portion I understand very well, it's completely legible, but not this part:

Code:
```>>> calculator = MuffledCalculator()
>>> calculator.calc('10/2')
5
>>> calculator.calc('10/0') # No muffling Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "MuffledCalculator.py", line 6, in calc
return eval(expr)
File "<string>", line 0, in ?
ZeroDivisionError: integer division or modulo by zero
>>> calculator.muffled = 1
>>> calculator.calc('10/0') Division by zero is illegal```
the first portion i again understand well, I even understand the exception and where I get confused is when he changes muffled to 1. I understand that python would return the ZeroDivisionError itself in trying to divide 0, but what does changing the value of muffled to 1 have to do with the exception when it's being muffled is dependent on the divisor being 0 because muffled=0 in the main program code? revertedly, shouldn't the first raised exception be in the case that muffled has been changed because of the try/except statement?

this by the way is the book i'm reading, from apress, every book i've read from apress has confused me no matter the topic, should have paid attention to that when i bought it

Magnus Lie Hetland. Beginning Python: From Novice to Professional (p. 163). Kindle Edition.
2. Code:
```class MuffledCalculator:
muffled = 0
def calc(self, expr):
try:
return eval(expr)
except ZeroDivisionError:
if self.muffled:
print 'Division by zero is illegal'
else:
raise```
Python syntax depends on leading white space and line breaks.
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2013
Posts
10
Rep Power
0

#### sorry

yeah sorry i didn't reproduce the book text properly, nonetheless i remain confused :P
4. ok, so you type in the expression 98/0
python raises an exception, which is caught by the try: except: business.
The logic in that block executes, and the behavior depends on the value of muffled.

muffled initially is a class-scope attribute. self.muffled first checks for an object-scope value of muffled. Not finding that python looks for the attribute in the class. Which it finds. Later you set MuffledCalculatorObject.muffled to 1. That's object-scope.

All these lookups explain several qualities of python:
1) Why the python programmers spend so much time making the dictionary hashes as efficient as possible;
2) How python is such a dynamic language.
Last edited by b49P23TIvg; December 27th, 2013 at 08:56 AM.
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2013
Posts
10
Rep Power
0
well when you speak to me of scope, I think of two things and the latter probably isn't related to scope. the first thing is the state that was made unclear in my other post, which is made according to the attributes of the program. scope in lay to me means the "range" in which a program executes, each instance of a class perhaps having it's own scope. that being said, the other thing that comes to mind when you say scope is range itself, like 0-9 or whatever number, index; etc. I still don't understand how changing muffled to 1 raises the exception because muzzle is looking for the value 0 in division. Changing it to one changes that value so when dividing by one, the original traceback that Python provides should occur and the try/except clause is passed so to speak, or at least it doesn't happen. Am I overcomplicating this then, I really don't enjoy this author's wording...
6. 3 scopes of python. With more of the story you'd learn to access them.
Code:
```variable_defined_in_module = 'has global scope'

def function():
variable_defined_in_function = 'has local scope'

class c:
attribute_defined_in_class = 'has class scope'```
7. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2013
Posts
10
Rep Power
0

#### ...

I got that part down as well, I just wanted to understand the fellas example, but I don't think it matters much so long as I have those portions of statefulness and scope down. thanks again.