Skip to content

Instantly share code, notes, and snippets.

@Bogdanp
Created December 27, 2015 12:22
Show Gist options
  • Save Bogdanp/47711e30301f9134f231 to your computer and use it in GitHub Desktop.
Save Bogdanp/47711e30301f9134f231 to your computer and use it in GitHub Desktop.
lol Python
import ast
import inspect
class NameVisitor(ast.NodeVisitor):
def __init__(self, default_value=None):
self.default_value = default_value
def visit_Name(self, node):
if isinstance(node.ctx, ast.Load):
if node.id not in globals():
globals()[node.id] = self.default_value
class PHPTon(object):
def show(self, x):
print "VALUE: {!r}".format(x)
def __getattribute__(self, name):
frame = inspect.stack()[1]
try:
call = ast.parse(frame[-2][0])
visitor = NameVisitor()
visitor.visit(call)
finally:
del frame
return super(PHPTon, self).__getattribute__(name)
p = PHPTon()
p.show(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment