在python中使用类变量作为非默认参数

cal*_*pto 8 python variables class default-value

有没有办法在类中保留一个私有类变量,并仍然使用它作为非默认变量的默认值(不在类之外定义该值)?

例:

class a:
    def __init__(self):
        self.__variable = 6
    def b(self, value = self.__variable):
        print value
Run Code Online (Sandbox Code Playgroud)

Amb*_*ber 14

你是在思考这个问题:

class a:

    def __init__(self):
        self.__variable = 6

    def b(self, value=None):
        if value is None:
            value = self.__variable
        print value
Run Code Online (Sandbox Code Playgroud)