Leading Underscores (was Re: [Zope] zope product problem)
Jim Washington
jwashin at vt.edu
Wed May 5 13:47:47 EDT 2004
Dominique Lederer wrote:
>
> > Did you create a new instance before clicking the "properties" tab?
> >
> > An old instance with a ._question property will not have a .question
> > property, unless you take special pains to update the old instance.
>
> thanks a lot, that helped!
>
> can you explain me in short words, when you use a ._question prop and
> when
> a .question prop ?
> what?s the difference ? i read it has something to do with Acquisition,
> but i didn't understand it.
> or maybe a link to a good explanation.
>
> greets HakTom
>
>
I could not find a really good link, and it would be a good idea to look
at the source of some of the Zope products out there, to get an idea of
"best practice."
In Zope2, names with leading underscores are considered private and are
not available through the web. You cannot access myObject._myVar in
DTML with <dtml-var _myVar>. myObject._myVar also is not available in
python script objects.
These variables are not completely unreachable, however. You can
provide public getter() and setter() methods with appropriate security
declarations for access or modification. This may be useful, for
example, if Zope's security does not do everything you need, or if you
need to do something special before modifying a value. That these names
do not participate in (unintended) acquisition may also be good.
So, in my opinion, if you need these values to be private, or if they
need special control, or if you think "explicit is better than implicit"
is a good idea for your product, use a leading underscore in the name.
It's less convenient to have to create setters and getters, but the
added security and predictability might be worth it.
Caveat: The leading-underscore-means-private policy probably will change
in Zope3.
Hope this helps.
-- Jim Washington
More information about the Zope
mailing list