[Tutor] Question on UserDict class - copy function
Rikard Bosnjakovic
rikard.bosnjakovic at gmail.com
Tue Apr 24 16:46:03 CEST 2007
On 4/24/07, Ketan Joshi <ktn_vrce at yahoo.com> wrote:
> If so, why isn't this function defined as:
>
> def copy(self):
> import copy
> return copy.copy(self)
The if-case in your code makes sure that the property __class__ is of
UserDict-inheritance. I believe it's there in case of multiple
classes.
class Gas:
def __init__(self):
pass
class Oil:
def __init__(self):
self.foo = Gas()
self.dict = UserDict()
b = Oil()
b.__class__ will be __main__.Oil, and if you pass the entire class
around and want a copy of the dict-property, the copy()-method in
UserDict will first it is a UserDict-instance before copying it. If it
isn't, it creates one out of the existing data.
--
- Rikard - http://bos.hack.org/cv/
More information about the Tutor
mailing list