[Zope] Problem with hasattr() and Zope 2.8.1

Doyon, Jean-Francois Jean-Francois.Doyon at CCRS.NRCan.gc.ca
Fri Sep 30 15:49:46 EDT 2005


Hello,

I'm using new-style classes and properties to implement multilingual support
in my objects. I might therefore have something like:

mything = property(__get_mything)

def __get_mything:
    return self.__mything_en

(Extremely simplified!)

This works fine.

Now however I'm discovering that doing a hasattr() on anything that starts
with 2 underscores always returns false!

So hasattr(self, '__thumbnail') doesn't work as expected, but hasattr(self,
'_thumbnail') DOES! (All other things being equal of course).

I've successfully tested this by replacing this code, which didn't work as
expected:

   def __get_thumbnail(self):
        if not hasattr(self, '__thumbnail'):
            self.setThumbnail()
            return self.__thumbnail
        else:
            return self.__thumbnail

With this one:

   def __get_thumbnail(self):
        try:
            return self.__thumbnail
        except:
            self.setThumbnail()
            return self.__thumbnail

Which DID work as expected (Though I suspect the try ... except might be
faster anyways!) ...

I checked the Collector:

http://www.zope.org/Collectors/Zope/collector_contents?searching=yep&Searcha
bleText=hasattr&status%3Alist%3Aignore_empty=Accepted&status%3Alist%3Aignore
_empty=Pending

Nothing jumped out at me as being relevant ...

Any ideas?

Jean-François Doyon
Internet Service Development and Systems Support / Spécialiste de
dèveloppements internet et soutien technique
Canada Centre for Remote Sensing/Centre Canadien de télédétection
Natural Resources Canada/Ressources Naturelles Canada
http://atlas.gc.ca
Tel./Tél.: (613) 992-4902
Fax: (613) 947-2410


More information about the Zope mailing list