On Mon, Dec 13, 2010 at 03:05:22AM -0800, Simon King wrote:
> At http://groups.google.com/group/sage-devel/browse_thread/thread/b730edf54d357e3b
> I asked about attribute lookup of elements and parents. It did not get
> a reply. Since it is related with categories, I ask here:
Thanks for the prompting! I am not able to follow sage-devel currently.
I agree with your e-mail. Just please make a quick benchmark (and post
the result on track) to verify that the extra check does not induce an
overhead for usual lookups (probably not). I'll try to review this
during the holidays, but it would be great if someone could take up
the task to get it done sooner.
Cheers,
Nicolas
> I learned that, in Python, attribute names starting with two
> underscores and not ending with an underscore are "private", hence,
> the convention is that they belong to a particular instance and not to
> its class.
>
> Currently, the attribute lookup for Parent and Element does not care
> about that convention:
Just a precision: for Parents and Elements implemented in Cython.
> sage: QQ.category().parent_class.__foo = 'bar'
> sage: QQ.__foo
> 'bar'
>
> Question:
> Should we obey the convention? I.e., should the __getattr__ method of
> Parent and Element immediately raise an error if a private attribute
> is requested, so that they are not looked up in the parent or element
> classes of the category?
>
> My vote is "Yes", for the following reasons:
>
> * The convention actually is obeyed by the Sage developers: All
> doctests still pass if one does the change.
>
> * Private attributes are typically used to store some data that are
> computed once and are very often requested afterwards. Example from
> SageObject.__repr__():
> if hasattr(self, '__custom_name'):
> return self.__custom_name
>
> Since it is clear that attributes like '__custom_name' will not be
> defined in the category, it would be a waste of time to try and look
> it up there.
>
> If you agree then please review #10467 ! If you disagree then please
> post at the ticket (or here) as well.
>
> Cheers,
> Simon
Nicolas
--
Nicolas M. Thi�ry "Isil" <nth...@users.sf.net>
http://Nicolas.Thiery.name/