Skip to content

Instantly share code, notes, and snippets.

@shomah4a
Created April 14, 2012 01:56
Show Gist options
  • Save shomah4a/2381462 to your computer and use it in GitHub Desktop.
Save shomah4a/2381462 to your computer and use it in GitHub Desktop.
python3 implicit __class__ variable
#-*- coding:utf-8 -*-
import traceback
class Base(object):
def test(self):
print('called')
class Test(Base):
def __init__(self):
print(__class__)
def call_super(self):
super().test()
def set_class(self):
''' ローカルスコープ外にある __class__ を書き換える '''
nonlocal __class__
__class__ = 10
def call_with_otherself(self):
''' super() の前に self を書き換える '''
self = 10
super()
def print_class(self):
print(__class__)
def catch(f):
try:
f()
except:
traceback.print_exc()
if __name__ == '__main__':
a = Test()
a.print_class()
catch(lambda: a.call_with_otherself())
a.call_super()
a.set_class()
a.print_class()
catch(lambda: a.call_super())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment