5

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()
Community
  • 1
  • 1
Heartagramir
  • 344
  • 1
  • 2
  • 9

1 Answers1

5

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

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
  • Well from what you said I understand they just copied the dict to use it but not to change it so why not: `self.params = params` instead? – Heartagramir Dec 12 '15 at 10:27
  • They do change it. Added a section about this to my answer. – Mike Müller Dec 12 '15 at 10:33
  • now I'v got it... in order to use a dict without changing it we use copy(). And thats not all... self.params wouldn't make a new variable for the class... it just copy the var to use it inside that specific object and still it's the same object. – Heartagramir Dec 12 '15 at 14:22