[Python-Dev] Re: PEP: Deferred Evaluation Of Annotations Using Descriptors

2021年1月15日 16:41:18 -0800

On 16/01/21 9:38 am, Guido van Rossum wrote:
On Fri, Jan 15, 2021 at 10:53 AM Larry Hastings <[email protected] <mailto:[email protected]>> wrote:
 class OuterCls:
   class InnerCls:
     def method(a:OuterCls.InnerCls=None): pass
 We've turned the local reference into a global reference, and we
 already know globals work fine.
I think this is going too far. A static method defined in InnerCls does not see InnerCls (even after the class definitions are complete). E.g.
```
class Outer:
   class Inner:
     @staticmethod
     def foo(): return Inner
```
If you then call Outer.Inner.foo() you get "NameError: name 'Inner' is not defined".
I'm not so sure about that. Conceptually, annotations are evaluated
in the environment existing when the class scope is being constructed.
The fact that we're moving them into a closure is an implementation
detail that I don't think should be exposed.
 What if we want to refer to something defined /after/
 the annotation?
 def outerfn():
   class OuterCls:
     class InnerCls:
       def method(a:zebra=None): pass
       ...
 We haven't seen the definition of "zebra" yet, so we don't know what
 approach to take.
I don't think that should be a problem. The compiler already knows
about all the assignments occurring in a scope before starting to
generate code for it.
--
Greg
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/SX66DYOEZSQCLY6JJMIDBS4LFHPB76Y3/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to