[Zope] Re: Problem with hasattr() and Zope 2.8.1

Doyon, Jean-Francois Jean-Francois.Doyon at CCRS.NRCan.gc.ca
Wed Oct 5 16:01:43 EDT 2005


Your example shows my problem, or what I see as a problem anyways:

....     def __init__(self):
....         self.__blech = 1

....     def do(self):
....         print hasattr(self, "__blech")

>>> x = Foo()
>>> x.do()
False

False? Really? hasattr() is used within the context of the class itself, so
the fact it's "private" shouldn't be a problem, should it? If I was using
hasattr() outside the scope/context of the class/instance, then ok ... But
within it? I'm not breaking any "privacy" rules by doing this ... I'd expect
the (un)mangling to be applied here.

Of course, hasattr() is a standalone function ... So maybe that's a factor.

Unintuitive behavior, I say ... Maybe I'll take this to c.l.p :)

So how does an instance test whether it has a private attribute set?  Is it
really proper for to unmangle "manually"? (This would strike me as bad form
when already working within the class/instance)

So then that leaves me wondering why my other pieces of code DON'T do this,
and work the way I initially expect ... No, my "lang" never ends in an "_"
... It's always __attname_en for example, or __attname_fr ... Etc ...

These are internal attributes wrapped by properties, though there is some
metaclass trickery going on here as well ... Might be a factor? The fact
some of the related logic (too long to show here, unless you're really
interested) is dynamically bound through metaclasses maybe changes the scope
of the attribute, and hence its mangling behavior?

Basically I use something like the "autoprop" examples from the Python docs
to do multilingual objects, where the language differences are handled at
the property level (i.e. the getters/setters handle language differences).

It's a pretty slick system if I may say so myself, works very well so far :)
Just don't want my hasattr() to blow up in my face at some unfortunate time!

Ah well, whatever it is, it's working ... I suppose I just don't understand
why ... But this isn't a Zope problem I guess :)

Thanks!
J.F.

-----Original Message-----
From: Paul Winkler [mailto:pw_lists at slinkp.com] 
Sent: October 5, 2005 3:28 PM
To: Jean-Francois.Doyon at CCRS.NRCan.gc.ca; zope at zope.org
Subject: Re: [Zope] Re: Problem with hasattr() and Zope 2.8.1

>>         for propertyname in [ propname for propname in
>> self.__multilingualproperties__.keys() if 
>> self.__multilingualproperties__[propname][1] == True ]:
>>             attname = '__' + propertyname + '_' + lang
>>             if hasattr(self, attname): delattr(self, attname)
>>
>> As you see on the last line, I'm doing a hasattr() on an attribute 
>> who's name starts with 2 underscores, and it works fine in this case 
>> (has been for weeks, if not months).
>
> But do those names *end* with underscores as well?

(snip)

Oops, sent too quickly; actually it looks to me like they don't (unless lang
ends in "__").
However, it's not clear what "it works" means.
Your code above won't raise any exceptions, but it shouldn't delete any
attributes either :-)

>>> class Foo:
....     def __init__(self):
....         self.__blech = 1
....         self.__blah__ = 1
....         self._bloop = 1
....     def do(self):
....         print hasattr(self, "__blech")
....         print hasattr(self, "_Foo__blech")
....         print hasattr(self, "__blah__")
....         print hasattr(self, "_bloop")
....
>>> x = Foo()
>>> x.do()
False
True
True
True

--
Paul Winkler
http://www.slinkp.com




More information about the Zope mailing list