[Edu-sig] re: Does edu-sig extend to Jython

Patrick K. O'Brien pobrien@orbtech.com
Mon, 19 Aug 2002 17:00:31 -0500


[Arthur]
>
> I get the gist of your suggestion, but the issue is pervasive and
> might overly tax my ingenuity.
>
> My current thinking is this:
>
> I need some testing of the arguments and some decent error
> reporting for inappropriate arguments, in any case. Right now
> you send a Line where the class needs a Plane, and somewhere down the
> pike you'll probably get a message about object x not having
> an attribute "normal" let's say -which Planes's have and Line's
> don't.  For me, as a user, I pretty now where to go. For
> someone else, as a user, it would be a mystery.
>
[snip]
>
> And in doing some real error catching and reporting on arguments,
> I would expect isinstance will come into play, and in
> doing it, it should not be very much additional burden to
> do it in such a way so that at least I can make argument
> order irrelevant.

Here is a less contrived example of a similar situation. I'm working on a
module used to generate proper xhtml documents in Python. To do this, I'm
applying a certain level of type checking for each level of a typical html
document hierarchy. For example, here is part of the Html class, which
corresponds to the html element, which can only contain one head and one
body element:

class Html(Element, AttrstringMixin):
    """HTML class."""

    name = 'html'
    format = '<html%(attributes)s>\n%(head)s%(body)s</html>\n'

    def __init__(self, head=None, body=None, lang='en', xmllang='en',
                 xmlns='http://www.w3.org/1999/xhtml', dir=None):

        if head is None:
            # Create a default head instance.
            self.head = Head()
        elif not isinstance(head, (str, Head)):
            raise TypeError, \
                  'head must be a string, a Head instance, or None'
        else:
            self.head = head

        if body is None:
            # Create a default body instance.
            self.body = Body()
        elif not isinstance(body, (str, Body)):
            raise TypeError, \
                  'body must be a string, a Body instance, or None'
        else:
            self.body = body

        self.attrs = {
            'lang': lang,         # Language.
            'xml:lang': xmllang,  # XML language.
            'xmlns': xmlns,       # XML namespace.
            'dir': dir,           # Direction.
            }

Elsewhere I've got a Head class and a Body class defined. As you can see,
I'm using isinstance() to guarantee that the proper type of objects are used
by the Head instance. (Of course, I've intentionally left a big back door
wide open by allowing a string as a valid parameter.)

Anyway, I hope this helps illustrate one way of making Python stricter than
usual. This module is a prototype and I'm playing around with various ideas
so don't necessarily look at this as a suggestion for THE way to solve the
problem, just one way that does work and isn't too hard to code.

--
Patrick K. O'Brien
Orbtech
-----------------------------------------------
"Your source for Python programming expertise."
-----------------------------------------------
Web:  http://www.orbtech.com/web/pobrien/
Blog: http://www.orbtech.com/blog/pobrien/
Wiki: http://www.orbtech.com/wiki/PatrickOBrien
-----------------------------------------------