Search code examples
pythondjangodictionaryshallow-copy

Why setting a dict shallow copy to itself?


I think it's a bit weird question to ask.
The thing is that while I was studying some parts of django code I came across something I've never seen before.
According to Copy Difference Question and It's usage in dictionary we can create two dictionary with same reference.

The question is what is the purpose of setting a shallow copy of a dictionary to itself?
Code:

django.template.backends.base

params = {
   'BACKEND' = 'Something',
   'DIRS' = 'Somthing Else',
}
params = params.copy()

Solution

  • The relevant part or django.template.backends.base.py looks like this:

    class BaseEngine(object):
    
        # Core methods: engines have to provide their own implementation
        #               (except for from_string which is optional).
    
        def __init__(self, params):
            """
            Initializes the template engine.
            Receives the configuration settings as a dict.
            """
            params = params.copy()
            self.name = params.pop('NAME')
            self.dirs = list(params.pop('DIRS'))
            self.app_dirs = bool(params.pop('APP_DIRS'))
            if params:
                raise ImproperlyConfigured(
                    "Unknown parameters: {}".format(", ".join(params)))
    

    The dictionary params in def __init__(self, params): will be copied to a new dictionary params = params.copy(). It just uses the same name. Therefore, the old object cannot be accessed any more via this name. In the next steps the new local dictionary is modified but the original one stays unchanged.

    Doing self.params = params, instead of params = params.copy() would have a very different effect. In this case self.paramswould be just a second name for the object behind params. Since it is a dictionary and mutable, all changes to self.params would effect params. params.pop('NAME') removes the key NAME' from the dictionary. Actually, there is a check that it is empty: params.pop('NAME').