开发者

Can you reference a class inside of itself in Python?

开发者 https://www.devze.com 2023-04-02 09:57 出处:网络
If I have classes A and B, I basically want to do something like this: class A: myDict = {A: 3, B: 2} Since my reference开发者_如何学JAVA isn\'t in a function, the class hasn\'t fully loaded by th

If I have classes A and B, I basically want to do something like this:

class A:
    myDict = {A: 3, B: 2}

Since my reference开发者_如何学JAVA isn't in a function, the class hasn't fully loaded by the time I try to use it. How can I get around this?


The proper solution is to move the dict outside the class

class A:
    pass

A.mydict = {A: 3, B: 2}

which will result in exactly the values you would get if you could do what you ask for.

0

精彩评论

暂无评论...
验证码 换一张
取 消