[Python-ideas] Fwd: Define a method or function attributeoutsideof a class with the dot operator

M.-A. Lemburg mal at egenix.com
Mon Feb 13 15:12:13 EST 2017


On 13.02.2017 20:32, Joseph Hackman wrote:
> I just wanted to ask: can someone point me to the reason Python doesn't support referencing a class inside it's own definition? It seems like that would solve some of the cases discussed here, and with Type hinting that seems like something that maybe should be considered?

The class doesn't exist yet, while Python is running the code
in its definition block.

You can play some tricks with meta classes exposing a .__prepare__()
method. This will receive the name of the to-be-created class
and allows returning a custom namespace in which the code is
run.

https://docs.python.org/3.6/reference/datamodel.html#preparing-the-class-namespace

The meta class docs have more details on how all this works:

https://docs.python.org/3.6/reference/datamodel.html#metaclasses

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Experts (#1, Feb 13 2017)
>>> Python Projects, Coaching and Consulting ...  http://www.egenix.com/
>>> Python Database Interfaces ...           http://products.egenix.com/
>>> Plone/Zope Database Interfaces ...           http://zope.egenix.com/
________________________________________________________________________

::: We implement business ideas - efficiently in both time and costs :::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/
                      http://www.malemburg.com/



More information about the Python-ideas mailing list