Newbie: static typing?

Ben Finney ben+python at benfinney.id.au
Tue Aug 6 18:34:20 EDT 2013


Rui Maciel <rui.maciel at gmail.com> writes:

> class AbstractVisitor(object):
>         def visit(self, element):
>                 pass

A small improvement to your code:

If you want an abstract method – that is, a method which should not be
called directly but which sub-classes should over-ride – then your
abstract method should not ‘pass’. Instead, it should ‘raise
NotImplementedError’ so this will be clear to anyone if the MRO falls
back to the abstract method.

> not_a_valid_type = "foo"
>
> model.accept(not_a_valid_type)

At this point, the ‘not_a_valid_type’ object is asked for its ‘visit’
method, and a TypeError is raised. That's good, because it informs the
person looking at the code that this object doesn't support the needed
behaviour.

This is as it's meant to be; what problem are you pointing out here?

-- 
 \      “Software patents provide one more means of controlling access |
  `\      to information. They are the tool of choice for the internet |
_o__)                                     highwayman.” —Anthony Taylor |
Ben Finney




More information about the Python-list mailing list