On 06/10/2010 04:32 PM, Az wrote:
> Let me take a guess:
>
> class Supervisor(object):
>       def __init__(self, ee_id, name, original_quota, loading_limit):
>               self.ee_id = ee_id
>               self.name = name
>               self.original_quota = original_quota
>               self.loading_limit = loading_limit
>               self.predecr_quota = 0
>               self.offered_proj = set()
>               self.total_prealloc_pop = 0
>               self.total_postalloc_pop = 0
>
>       def __repr__(self):
>               return str(self)
>
>       def __str__(self):
>               return self.name
>               return "%s %s %s (Offered projects: %s)" %(self.ee_id, 
> self.name,
> self.predecr_quota, self.offered_proj)
>
> So *inside* the Supervisor class would I define it like this (trying
> to have a go at it)?
>
> def __deepcopy__(self, memo):
>     dc = type(self)()
>     dc.__dict__.update(self.__dict__)
>     for attr in dir(supervisor):
>         if not attr.startswight('__'):
>             self.attr = deepcopy(self.attr, memo)
>   

The location of the __deepcopy__ method is correct, but there are
several problems with the implementation:

   1. You are modifying self, when you want to modify dc.
   2. You are not returning dc.
   3. dir(supervisor) (I assume you meant dir(self)) will include
      attributes from the class object itself, but you don't want to
      make copies of those attributes. I would recommend iterating over
      self.__dict__ instead or manually specifying the attributes to be
      copied.
   4. attr.startswith('__') will not catch '_sa_instance_state': you
      probably want attr.startswith('_') or attr.startswith('_sa_') instead.

> So this only overrides __deepcopy__ when I call it for a Supervisor
> and not for any of the other classes right?
>   

Correct.

-Conor

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to