apostofes
apostofes

Reputation: 3703

How do I get LOAD_CLASSDEREF instruction after dis.dis?

What function / class should I write to get LOAD_CLASSDEREF as a bytecode instruction?

I have been able to find functions / classes that result in LOAD_BUILD_CLASS, LOAD_CONST, LOAD_GLOBAL, LOAD_FAST, LOAD_ATTR, LOAD_DEREF, LOAD_NAME, bytecode, but what function / class would give LOAD_CLOSURE and LOAD_CLASSDEREF?

Upvotes: 2

Views: 169

Answers (1)

user2357112
user2357112

Reputation: 281013

LOAD_CLASSDEREF is used for when a class body accesses a closure variable, so have a class body access a closure variable:

def foo():
    x = 3
    class Bar:
        print(x)

The code object for Bar will then use LOAD_CLASSDEREF. Depending on your Python version, you may have to dig the code object out of foo.__code__.co_consts and call dis.dis on that code object directly to see the opcode in the dis.dis output.

You can also see a LOAD_CLOSURE in the bytecode for foo.

Upvotes: 3

Related Questions