"Dave Angel" <[email protected]> wrote

def result_of_SPECIALIZED_SQLcall_for_child():
    name =None
    return name

I assume this is a placeholder for more complex code to follow?

class Child(Parent):
    def add_name(self):
        name = result_of_SPECIALIZED_SQLcall_for_child()
        try:
            name + ''

This does nothing except raise a type error. But that's a very contrived way of checking the type.

            self.name =ame

I assume this is a typo? And it never gets executed because the error is always raised.

        except TypeError:
            super(Child, self).add_name()

I know this is a simplified example, but I'd still like to point out that using exceptions when there's a simple test is not reasonable. You can just check for None with a simple if test.

I don't mind using exceptions for a simple test but not where the test is being forced by a piece of code that does nothing. In this case I agree it would be better to check explicitly for a string if a string is required.

Even using exceptions it would be better to do someting like:

try:
  if type(name) != str:
     raise TypeError
  # other stuff here
except TypeError:
   super(....)

It makes the purpose of the code obvious.

But

  if type(name) != str:
       super(....)
  else:
       # other stuff here

is clearer still


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to