First, I know how copy and deepcopy from copy module work for a list.


I'm interested the role of __deepcopy special method inside a class.


I need to create custom widgets in Django and I try to understand the 
python code behind.


class Widget(metaclass=MediaDefiningClass):

    needs_multipart_form = False  # Determines does this widget need multipart 
form
    is_localized = False
    is_required = False
    supports_microseconds = True  # for data and time values

    def __init__(self, attrs=None):
        if attrs is not None:
            self.attrs = attrs.copy()
        else:
            self.attrs = {}

    def __deepcopy__(self, memo):
        obj = copy.copy(self)
        obj.attrs = self.attrs.copy()
        memo[id(self)] = obj
        return obj

What is happening with *__deepcopy__* when I make an instance of Widget, or 
and instance of a subclass of Widget ? Looks like is making a shallow copy 
of attributes and the object itself , but what is the purpose ?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/7d313c90-4407-4f9f-9518-de5327d14178%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to