Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
like image 391
Heartagramir Avatar asked Dec 12 '15 10:12

Heartagramir


People also ask

Why would you want a shallow copy?

Shallow copies are useful when you want to make copies of classes that share one large underlying data structure or set of data.

Does changing a shallow copy change the original?

In the case of shallow copy, a reference of an object is copied into another object. It means that any changes made to a copy of an object do reflect in the original object.

What is the point of shallow copy Python?

A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.

What are problems with shallow copy?

The problem with the shallow copy is that the two objects are not independent. If you modify the one object, the change will be reflected in the other object. A deep copy is a fully independent copy of an object. If we copied our object, we would copy the entire object structure.


1 Answers

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').

like image 157
Mike Müller Avatar answered Oct 15 '22 01:10

Mike Müller