Skip to content

Instantly share code, notes, and snippets.

@markrwilliams
Created May 11, 2013 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markrwilliams/5561363 to your computer and use it in GitHub Desktop.
Save markrwilliams/5561363 to your computer and use it in GitHub Desktop.
import sys
import opcode
import struct
OPNAME = struct.Struct('B')
ARG = struct.Struct('H')
class Symbol(object):
def __init__(self):
caller_frame = sys._getframe(1)
ic = caller_frame.f_lasti
code = caller_frame.f_code.co_code
names = caller_frame.f_code.co_names
(op,) = OPNAME.unpack(code[ic])
assert opcode.opname[op] == 'CALL_FUNCTION'
ic += (OPNAME.size + ARG.size)
(op,) = OPNAME.unpack(code[ic])
if opcode.opname[op] != 'STORE_NAME':
raise RuntimeError("Must assign symbol a name!")
ic += OPNAME.size
(idx,) = ARG.unpack(code[ic:ic + ARG.size])
self.name = names[idx]
def __repr__(self):
return "<Symbol '{}>".format(self.name)
MetaInfo = Symbol()
d = {MetaInfo: 1, 'a': 2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment