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

Steven D'Aprano steve at pearwood.info
Mon Feb 13 19:26:12 EST 2017


On Mon, Feb 13, 2017 at 02:32:33PM -0500, Joseph Hackman wrote:

> Generally speaking, I'm +1 on this idea, I think it would make code 
> more readable, especially for tools like IDEs.
> 
> 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 simple answer is: since the class doesn't exist yet, you cannot 
refer to it. The class name is just a regular name:

py> MyClass = 'something else'
py> class MyClass:
...     print(MyClass)
...
something else

so the interpreter would need to provide some special-cased magic inside 
the class body to make it work as you expect. That may be a good idea, 
but it is a separate issue from this.



-- 
Steve


More information about the Python-ideas mailing list