Skip to content

super and __class__ special case #13

@bjpop

Description

@bjpop

super and class are special cased in Python 3 classes.

See: http://docs.python.org/3/reference/datamodel.html#creating-the-class-object

"This class object is the one that will be referenced by the zero-argument form of super(). class is an implicit closure reference created by the compiler if any methods in a class body refer to either class or super. This allows the zero argument form of super() to correctly identify the class being defined based on lexical scoping, while the class or instance that was used to make the current call is identified based on the first argument passed to the method."

See also: http://www.python.org/dev/peps/pep-3135/#super-used-with-call-attributes

And in Python/symtable.c:

    /* Special-case super: it counts as a use of __class__ */
    if (e->v.Name.ctx == Load &&
        st->st_cur->ste_type == FunctionBlock &&
        !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
        if (!GET_IDENTIFIER(__class__) ||
            !symtable_add_def(st, __class__, USE))
            return 0;
    }

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions