UniqueRepresentation in FormalSums

53 views
Skip to first unread message

Martin R

unread,
Jan 13, 2016, 3:15:54 PM1/13/16
to sage-devel
Hi there!

I'm trying to understand inheritance.  In formal_sum I find the following code:

class FormalSums(UniqueRepresentation, Module):

    Element = FormalSum

    def _element_constructor_(self, x, check=True, reduce=True):

        if isinstance(x, FormalSum):
            P = x.parent()
            if P is self:
                return x
            elif P == self:
                return self.element_class(x._data, check=False, reduce=False, parent=self)
            else:
                x = x._data

gives me the impression that "P is self" and "P == self" should be the same thing.  Is my impression wrong or is the code above misleading?

Thanks,

Martin

Justin C. Walker

unread,
Jan 13, 2016, 3:31:28 PM1/13/16
to SAGE Development
To understand the difference between ‘is’ and ‘==‘ may require some background in programming.

Specifically, “a==b” evaluates to “True” if the objects ‘a’ and ‘b’ “evaluate to the same *value*”, while “a is b” evaluates to “True” if the objects ‘a’ and ‘b’ *are the same” (i.e., are located at the same locations in RAM).

If that is not clear (and it need not be; it’s a kind of complicated issue in programming language design and implementation), do an internet search for something like “is vs == in python”.

To answer your questions, your impression is not correct (but you are not alone), and the code is not misleading (making the rash assumption that it is correct, which I have not verified).

HTH

Justin

--
Justin C. Walker, Curmudgeon at Large
Director
Institute for the Enhancement of the Director's income
-----------
--
They said it couldn't be done, but sometimes,
it doesn't work out that way.
  - Casey Stengel
--



Martin R

unread,
Jan 13, 2016, 3:43:25 PM1/13/16
to sage-devel
To understand the difference between ‘is’ and ‘==‘ may require some background in programming.

Specifically, “a==b” evaluates to “True” if the objects ‘a’ and ‘b’ “evaluate to the same *value*”, while “a is b” evaluates to “True” if the objects ‘a’ and ‘b’ *are the same” (i.e., are located at the same locations in RAM).

Yes, I understand this.  (and to make sure, I checked the python doc)

To answer your questions, your impression is not correct (but you are not alone), and the code is not misleading (making the rash assumption that it is correct, which I have not verified).

Now I do not understand.  FormalSums inherits from UniqueRepresentation.  The documentation says:

    Instances of a class have a unique representation behavior when instances evaluate equal if and only if they are identical (i.e., share the same memory representation), if and only if they were created using equal arguments.

So, suppose I have two instances A and B of FormalSums.  Then, I would think that A == B holds if and only if A is B.  Or is "evaluate equal" and "A==B" different?

Many thanks!

Martin

Daniel Krenn

unread,
Jan 13, 2016, 3:56:52 PM1/13/16
to sage-...@googlegroups.com
I believe you are right. P is self and P == self is equivlaent when
using UniqueRepresentation (at least in my understanding and appearently
in yours as well).
(To be more sure, add a
print "never"
after the "elif P == self" and run the doctests. None should fail...

Best

Daniel

John H Palmieri

unread,
Jan 13, 2016, 3:57:00 PM1/13/16
to sage-devel


On Wednesday, January 13, 2016 at 12:43:25 PM UTC-8, Martin R wrote:
To understand the difference between ‘is’ and ‘==‘ may require some background in programming.

Specifically, “a==b” evaluates to “True” if the objects ‘a’ and ‘b’ “evaluate to the same *value*”, while “a is b” evaluates to “True” if the objects ‘a’ and ‘b’ *are the same” (i.e., are located at the same locations in RAM).

Yes, I understand this.  (and to make sure, I checked the python doc)

To answer your questions, your impression is not correct (but you are not alone), and the code is not misleading (making the rash assumption that it is correct, which I have not verified).

Now I do not understand.  FormalSums inherits from UniqueRepresentation.

"FormalSums" inherits from UniqueRepresentation. "FormalSum" does not, as far as I can tell. So for instances of FormalSum, which are what arise in the snippet you posted, "==" may not be the same as "is".

--
John

Martin R

unread,
Jan 13, 2016, 4:03:15 PM1/13/16
to sage-devel
Now I am entirely confused.  I read:

        if isinstance(x, FormalSum):
            P = x.parent()

I would think that P is now an instance of "FormalSums", not an instance of "FormalSum"?

John H Palmieri

unread,
Jan 13, 2016, 4:20:26 PM1/13/16
to sage-devel

Oh, you're right. What happens if you follow Daniel Krenn's suggestion to change the "elif P == self" block and run doctests?

--
John

Martin R

unread,
Jan 14, 2016, 4:25:34 AM1/14/16
to sage-devel
Reply all
Reply to author
Forward
0 new messages