python class variable not visible in __init__?

前端 未结 4 1249
孤街浪徒
孤街浪徒 2021-02-06 01:47

This code produces an error message, which I found surprising:

class Foo(object):
    custom = 1
    def __init__(self, custom=Foo.custom):
        self._custom          


        
4条回答
  •  不思量自难忘°
    2021-02-06 02:40

    The class body is executed before the class its self is defined, so default argument values can't reference the class. Just making custom the default (without class qualification) should work.

提交回复
热议问题