(original) (raw)

(sorry for obvious mistakes in the example in previous e-mail)

On 14 September 2017 at 21:08, Ivan Levkivskyi <levkivskyi@gmail.com> wrote:
On 14 September 2017 at 01:13, Guido van Rossum <guido@python.org> wrote:

That last sentence is a key observation. Do we even know whether there are (non-toy) things that you can do \*in principle\* with \_\_class\_\_ assignment but which are too slow \*in practice\* to bother? And if yes, is \_\_getattr\_\_ fast enough? @property?


I myself have never implemented deprecation warnings management nor lazy loading,
so it is hard to say if \_\_class\_\_ assignment is fast enough. For me it is more combination
of three factors:

\* modest performance improvement
\* some people might find \_\_getattr\_\_ clearer than \_\_class\_\_ assignment
\* this would be consistent with how stubs work
IMO we're still looking for applications.


How about this

def allow\_forward\_references(\*allowed):
caller\_globals = sys.\_getframe().\_\_globals\_\_
def typing\_getattr(name):
if name in allowed:
return name
raise AttributeError(...)
caller\_globals.\_\_getattr\_\_ = typing\_getattr

from typing\_extensions import allow\_forward\_references
allow\_forward\_references('Vertex', 'Edge')

T = TypeVar('T', bound=Edge)

class Vertex(List\[Edge\]):
def copy(self: T) -> T:
...

class Edge:
ends: Tuple\[Vertex, Vertex\]
...

Look mum, no quotes! :-)

--
Ivan