On Thu, Jun 4, 2015 at 11:16 AM, David Roe <roed.m...@gmail.com> wrote: > I would say that it's fine, especially in a subclass. > David >
And I would say it's not fine. If you have to explicitly de-mangle an attribute name, then the overall code is structured badly. The whole point of __ attributes in Python is to hide internal implementation details. Incidentally, if you refer to a private dunder'd attribute from the class in which it is defined, then you do *not* have to demangle it. Your question seems to suggest maybe you don't realize this, since you say "Thus if I implement a method `number_of_terms` in the class `Polynomial_generic_sparse`" and also you write "p._Polynomial_generic_sparse__coeffs". If you try to get at __coeffs from outside the class then you have to mangle -- but no mangling is needed from within the class. William > > On Thu, Jun 4, 2015 at 9:23 AM, Bruno Grenet <bruno.gre...@gmail.com> wrote: >> >> Dear all, >> >> Many classes in SageMath (most of them? all of them? I don't know...) have >> their attributes hidden by a leading `__`. Yet in Python hidden attributes >> are never really hidden, since it is possible to access the attribute >> `__hidden` of the class `MyClass` as `_MyClass__hidden`. >> >> What SageMath's policy about using these hidden attributes in functions? >> >> A concrete example: >> >> For sparse polynomials (class `Polynomial_generic_sparse` in >> `src/sage/rings/polynomial/polynomial_element_generic.py`), the coefficients >> are stored as a dictionary, stored in the attribute `__coeffs`. To compute >> the number of nonzero monomials, one can use any of the following three >> solutions: >> >> sage: len(p.coefficients()) # builds the list of (nonzero) coefficients, >> and computes its length >> sage: len(p.dict()) # copies the dictionary __coeffs and computes its >> length >> sage: len(p._Polynomial_generic_sparse__coeffs) # computes the length of >> __coeffs >> >> As one can expect, the fastest is solution 3. To get an idea of the >> differences, with a random degree-10000 polynomial over ZZ, I get using >> `timeit`: >> >> 1.11 ms >> 174 µs >> 88 ns (with slowest run 46.05 times longer than fastest) >> >> Thus if I implement a method `number_of_terms` in the class >> `Polynomial_generic_sparse`, I am tempted to implement the third solution. >> But is referring to a hidden attribute an issue? >> >> Cheers, >> Bruno >> >> -- >> You received this message because you are subscribed to the Google Groups >> "sage-devel" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to sage-devel+unsubscr...@googlegroups.com. >> To post to this group, send email to sage-devel@googlegroups.com. >> Visit this group at http://groups.google.com/group/sage-devel. >> For more options, visit https://groups.google.com/d/optout. > > > -- > You received this message because you are subscribed to the Google Groups > "sage-devel" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sage-devel+unsubscr...@googlegroups.com. > To post to this group, send email to sage-devel@googlegroups.com. > Visit this group at http://groups.google.com/group/sage-devel. > For more options, visit https://groups.google.com/d/optout. -- William (http://wstein.org) -- You received this message because you are subscribed to the Google Groups "sage-devel" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+unsubscr...@googlegroups.com. To post to this group, send email to sage-devel@googlegroups.com. Visit this group at http://groups.google.com/group/sage-devel. For more options, visit https://groups.google.com/d/optout.