How does the function that is called inside the class declaration?

前端 未结 2 925
-上瘾入骨i
-上瘾入骨i 2020-12-19 19:18

Have this code:

>>> class Foo:
...     zope.interface.implements(IFoo)
...
...     def __init__(self, x=None):
...         self.x = x
...
...     de         


        
2条回答
  •  感动是毒
    2020-12-19 19:42

    Read the source, luke:

    http://svn.zope.org/zope.interface/trunk/src/zope/interface/declarations.py?rev=124816&view=markup

    def _implements(name, interfaces, classImplements):
        frame = sys._getframe(2)
        locals = frame.f_locals
    
        # Try to make sure we were called from a class def. In 2.2.0 we can't
        # check for __module__ since it doesn't seem to be added to the locals
        # until later on.
        if (locals is frame.f_globals) or (
            ('__module__' not in locals) and sys.version_info[:3] > (2, 2, 0)):
            raise TypeError(name+" can be used only from a class definition.")
    
        if '__implements_advice_data__' in locals:
            raise TypeError(name+" can be used only once in a class definition.")
    
        locals['__implements_advice_data__'] = interfaces, classImplements
        addClassAdvisor(_implements_advice, depth=3)
    

提交回复
热议问题