On Aug 21, 2007, at 2:14 PM, Brendan Arnold wrote:

>
> hi there,
>
> i'd like to add an array of 'tag' attributes for use on my orm objects
> while my scripts are running (i.e. a flag to delete etc.).
>
> recently i found that,
>
> class Foo(object):
>   tags = []
>
> caused problems with object instances sharing the same .tags attribute
> list. to ensure that a new instance was created on instantiation i
> started doing,
>
> class Foo(object):
>   def __init__(self):
>     self.tags = []
>
> however the init behaviour of sqlalchemy is altered, see
> http://www.sqlalchemy.org/trac/wiki/ 
> FAQ#whyisntmy__init__calledwhenIloadobjects
>
> i realise that there is a way around this described in the sqlalchemy
> faq, but since i am a relative python newbie i was wondering if there
> is a better way to ensure each one of my instances has a fresh list
> created?
>

a common technique is to use a property:

class MyClass(object):
      def _init_attr(self):
         if not hasattr(self, '_foo'):
             self._foo = []
         return self._foo
     foo = property(_init_attr)



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
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