Black Magic Collection for the creation of classes omit self
From types import functiontypefrom byteplay import Code, Opmapdef metaclassfactory (function): Class Metaclass (Type): def __new__ (Meta, classname, bases, classdict): For AttributeName, attribute in Classdict.items (): If type (attribute) = = Functiontype:attribute = function (attribute) newclassdict [AttributeName] = attribute return type.__new__ (meta, classname, bases, classdict) return metaclassdef _tra nsmute (opcode, arg): if ((opcode = = opmap[' Load_global ') and (arg = = ' self '): return opmap[' Load_fast ') , arg return opcode, argdef selfless (function): Code = Code.from_code (function.func_code) Code.args = tuple ([' Sel F '] + list (Code.args)) Code.code = [_transmute (OP, Arg) for OP, arg in code.code] Function.func_code = Code.to_code ( return functionselfless = Metaclassfactory (selfless) class Test (object): __metaclass__ = selfless def __init__ (x =none): self.x =x def getX (): Return self.x def setX (x): self.x = Xtest = Test () print (Test.getx ()) Test.setx (7) print (Te St.getx ())
Python black Magic Collection