0

The following is part of SqlAlchemy, I'm trying to figure out how this is valid Python syntax. I know the code works, I'm just trying to understand what is is doing here and how that is valid. These are NOT nested classes, they are stand alone classes. I would have expected a pass after each class declaration though and I'm pretty sure it not being there is significant.

class InstrumentedList(list):
    """An instrumented version of the built-in list."""


class InstrumentedSet(set):
    """An instrumented version of the built-in set."""


class InstrumentedDict(dict):
    """An instrumented version of the built-in dict."""


__canned_instrumentation = {
    list: InstrumentedList,
    set: InstrumentedSet,
    dict: InstrumentedDict,
}

__interfaces = {
    list: (
        {'appender': 'append', 'remover': 'remove',
         'iterator': '__iter__'}, _list_decorators()
    ),

    set: ({'appender': 'add',
           'remover': 'remove',
           'iterator': '__iter__'}, _set_decorators()
          ),

    # decorators are required for dicts and object collections.
    dict: ({'iterator': 'values'}, _dict_decorators()) if util.py3k
    else ({'iterator': 'itervalues'}, _dict_decorators()),
}
2
  • 4
    so what exactly is unclear to you? Commented Mar 15, 2018 at 17:59
  • 7
    The class body has to contain something, but a docstring counts as that something. It's a statement, like pass. Commented Mar 15, 2018 at 18:03

1 Answer 1

1

It is valid python syntax as long as there is something inside of the class definition.

For example:

class FooBar:
    """ foobar """

This is okay because there is something (in this case a docstring, which can be accessed with the __doc__ attribute) in the class definition.

However, a class definition without anything in it is invalid syntax:

class FooBar:

When this is run, it produces the output:

SyntaxError: unexpected EOF while parsing
Sign up to request clarification or add additional context in comments.

1 Comment

I guess I'm still thinking about comments like C/C++ as things that aren't code. I have a new appreciation for docstrings now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.