You need to use a global statement to pull the module-level global a variable into your scope:
a = 2
class adder():
def __init__(self):
global a # need this!
# a = a # don't need this
a = a % 5 # assigning to `a` would be otherwise ambiguous without
# the global statement
print a
You don't need the semicolons.
Better is to use a default argument, something like this, to avoid changing things on the global level.:
a = 2
class adder():
def __init__(self, a=a):
# don't need global statement since we're going to default to
# the global a.
# a = a # don't need this
a = a % 5
print a
Another superior approach would be to use different names on the global and local level. I would rename the global variable, and keep the local variable the same. We tend to capitalize (all caps) global constants (and capitalize class names, and inherit from object), so that might look like this:
A = 2
class Adder(object):
def __init__(self):
a = A % 5
print a