In my introduction to computer science class we are learning about namespaces. And I understand the concept as in when importing a module like math, then we are importing a namespace and the, the class attributes under that namespace, but executing the process is very confusing to me. Here is one of the questions that I have no idea how to begin executing:
Write a function, name_add(a,b), that uses exception handling (just a simple try/except statement) to add two objects, a and b, and return the result. If the user calls the function with any types for which the + operator is not defined, the function should print a message stating that the addition operator is undefined between type(a) and type(b) (whatever those types are)......l
If someone could explain step by step what this function should look like or what they are asking in beginners terms, I would greatly appreciate it because I am not really understanding this at all or the relation it has to namespaces.
You need to catch the TypeError exception. This is the answer to the question:
def name_add(a,b):
try:
return a+b
except TypeError:
print 'The + operator is not defined for a and b'
return None
As Lattyware commented, catching the exception and just printing a message is not a good practice. You should either:
Look at this fragment of one of my programs. This is the top level function:
def main(argv):
"""Main program for the nc2pdf utility.
:argv: command line arguments
"""
if len(argv) == 1: # No filenames given, only the name of the script
binary = os.path.basename(argv[0])
print __proginfo__
print "Usage: {} [file ...]".format(binary)
print
sys.exit(0)
del argv[0]
for fn in argv: # Loop over all the files
try:
ofn = outname(fn) # outname can raise ValueError...
with open(fn, 'r') as inf: # Open can raise IOError
rd = inf.read()
except ValueError:
fns = "Cannot construct output filename. Skipping file '{}'."
print fns.format(fn)
continue
except IOError:
print "Cannot open the file '{}'. Skipping it.".format(fn)
continue
... # do something with the file's data
In this case, the exception can be handled by skipping (not processing) one of the files named on the command line and moving on to the next file. Not handling the exception here would crash the program, even though other files might still be processed. A filename can be misspelled, or the process may not have access permission to the file. These things happen and should be handled gracefully.
To modify a variable declared outside of a function, you have to use the global keyword in the function:
a = 1
def mod(b):
global a
a = b
mod(2)
print a
#prints 2
Note: this applies for immutable types (strings, numbers, booleans, tuples), but for lists:
a = [1,2]
def mod(b):
a[0] = b
mod(2)
print a
#prints [2,2]
For the problem you mention in the body of your question (which is somehow totally different than the title) I recommend the answer of Ronald Smith.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With