Re: [Zope] Problem with hasattr() and Zope 2.8.1
On Friday 30 September 2005 12:49 pm, Doyon, Jean-Francois wrote: > 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). This is actually a feature of python. Names starting with '__' are mangled by the interpreter so that they are not directly accessible outside the class itself; it is an attempt to simulate private class members. If you are finding yourself in need of using a variable that someone else has decided needed to be named with '__' then you may want to rethink what you are doing. It is somewhat rare that a python programmer would use this trick, so you should probably heed the warning and avoid using it if at all possible. If these are methods that you created then just rename them to use a single or preferably no underscores if you need to use them from outside the class itself (a single underscore is still bad form, as '_' is an indicator that a variable is intended to be private and a suggestion that it only be used in the class itself). Alec Mitchell ___ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
[Zope] Problem with hasattr() and Zope 2.8.1
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 ___ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )