Method's default parameter values are evaluated *once*

后端 未结 3 1708
广开言路
广开言路 2020-12-06 17:39

I\'ve found a strange issue with subclassing and dictionary updates in new-style classes:

Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit          


        
3条回答
  •  -上瘾入骨i
    2020-12-06 18:18

    props should not have a default value like that. Do this instead:

    class a(object):
        def __init__(self, props=None):
            if props is None:
                props = {}
            self.props = props
    

    This is a common python "gotcha".

提交回复
热议问题