小编dam*_*ian的帖子

Python __init__参数问题

我很难理解类init的参数会发生什么,
例如:

class A(object):
    def __init__(self, argument=[]):  
        self.argument = argument[:]  
Run Code Online (Sandbox Code Playgroud)

要么:

def __init__(self,argument=None):  
    self.arguments = arguments or []  
Run Code Online (Sandbox Code Playgroud)

要么:

def __init__(self, argument=[]):  
    self.argument = argument  
Run Code Online (Sandbox Code Playgroud)

这是不可能的,因为每个A对象的默认值都指向同一块内存.我真的不明白这里发生了什么以及它是如何发生的.

python identity equality class list

3
推荐指数
1
解决办法
3724
查看次数

标签 统计

class ×1

equality ×1

identity ×1

list ×1

python ×1