[Zope-Coders] SimpleItem.getId busted

Florent Guillaume fg at nuxeo.com
Thu Nov 20 13:41:22 EST 2003


In article <20031120094031.53ec1b79.casey at zope.com> you write:
> >      def getId(self):
> >          """Return the id of the object as a string. This method
> >             should be used in preference to accessing an id attribute
> >             of an object directly. The getId method is public."""
> >          name=getattr(self, 'id', None)
> >          if callable(name):
> >              return name()
> >          if name is not None:
> >              return name
> >          if hasattr(self, '__name__'):
> >              return self.__name__
> >          raise AttributeError, 'This object has no id'
> > to:
> > 
> > 	def getId(self):
> >          """Return the id of the object as a string. This method
> >             should be used in preference to accessing an id attribute
> >             of an object directly. The getId method is public."""
> > 		name = getattr(aq_base(self), 'id', None)
> >	    if callable(name):
> > 		return name()
> > 	    if name:
> > 		return name
> >         try:
> >             return self.__name__
> >         except AttributeError:
> >             raise AttributeError, 'This object has no id'
>
> def getId(self):
>     try:
>         name = aq_base(self).id
>     except AttributeError:
>         try:
>             return self.__name__
>         except AttributeError:
>             raise AttributeError, 'This object has no name'
>     else:
>         try:
>             return name()
>         except TypeError:
>             return name

Beware, if id is callable you'll end up calling aq_base(self).id() and
not self.id() which can make a big difference...

Florent

-- 
Florent Guillaume, Nuxeo (Paris, France)
+33 1 40 33 79 87  http://nuxeo.com  mailto:fg at nuxeo.com



More information about the Zope-Coders mailing list