Difference between dict.clear() and assigning {} in Python

后端 未结 8 914
我在风中等你
我在风中等你 2020-11-28 19:57

In python, is there a difference between calling clear() and assigning {} to a dictionary? If yes, what is it? Example:

d = {\"stuff\":\"         


        
相关标签:
8条回答
  • 2020-11-28 20:14

    Mutating methods are always useful if the original object is not in scope:

    def fun(d):
        d.clear()
        d["b"] = 2
    
    d={"a": 2}
    fun(d)
    d          # {'b': 2}
    

    Re-assigning the dictionary would create a new object and wouldn't modify the original one.

    0 讨论(0)
  • 2020-11-28 20:22

    One thing not mentioned is scoping issues. Not a great example, but here's the case where I ran into the problem:

    def conf_decorator(dec):
        """Enables behavior like this:
            @threaded
            def f(): ...
    
            or
    
            @threaded(thread=KThread)
            def f(): ...
    
            (assuming threaded is wrapped with this function.)
            Sends any accumulated kwargs to threaded.
            """
        c_kwargs = {}
        @wraps(dec)
        def wrapped(f=None, **kwargs):
            if f:
                r = dec(f, **c_kwargs)
                c_kwargs = {}
                return r
            else:
                c_kwargs.update(kwargs) #<- UnboundLocalError: local variable 'c_kwargs' referenced before assignment
                return wrapped
        return wrapped
    

    The solution is to replace c_kwargs = {} with c_kwargs.clear()

    If someone thinks up a more practical example, feel free to edit this post.

    0 讨论(0)
  • 2020-11-28 20:23

    As an illustration for the things already mentioned before:

    >>> a = {1:2}
    >>> id(a)
    3073677212L
    >>> a.clear()
    >>> id(a)
    3073677212L
    >>> a = {}
    >>> id(a)
    3073675716L
    
    0 讨论(0)
  • 2020-11-28 20:24

    d = {} will create a new instance for d but all other references will still point to the old contents. d.clear() will reset the contents, but all references to the same instance will still be correct.

    0 讨论(0)
  • 2020-11-28 20:25

    In addition to the differences mentioned in other answers, there also is a speed difference. d = {} is over twice as fast:

    python -m timeit -s "d = {}" "for i in xrange(500000): d.clear()"
    10 loops, best of 3: 127 msec per loop
    
    python -m timeit -s "d = {}" "for i in xrange(500000): d = {}"
    10 loops, best of 3: 53.6 msec per loop
    
    0 讨论(0)
  • 2020-11-28 20:28

    In addition to @odano 's answer, it seems using d.clear() is faster if you would like to clear the dict for many times.

    import timeit
    
    p1 = ''' 
    d = {}
    for i in xrange(1000):
        d[i] = i * i
    for j in xrange(100):
        d = {}
        for i in xrange(1000):
            d[i] = i * i
    '''
    
    p2 = ''' 
    d = {}
    for i in xrange(1000):
        d[i] = i * i
    for j in xrange(100):
        d.clear()
        for i in xrange(1000):
            d[i] = i * i
    '''
    
    print timeit.timeit(p1, number=1000)
    print timeit.timeit(p2, number=1000)
    

    The result is:

    20.0367929935
    19.6444659233
    
    0 讨论(0)
提交回复
热议问题