On Mon, Jun 2, 2008 at 10:30 PM, Robert Bradshaw
<[EMAIL PROTECTED]> wrote:
>
> On Jun 2, 2008, at 10:18 PM, William Stein wrote:
>
>> On Mon, Jun 2, 2008 at 10:15 PM, Robert Bradshaw
>> <[EMAIL PROTECTED]> wrote:
>>>
>>> On Jun 2, 2008, at 9:51 PM, Igor Tolkov wrote:
>>>
>>>> I am trying to extend the integer class:
>>>>
>>>> {{
>>>> class Quple(Integer):
>>>>     def __init__(self, a, b):
>>>>         Integer.__init__(self, b)
>>>>         self.tup = (a, b)
>>>>
>>>> print Quple(3,1)
>>>> ///
>>>>
>>>> 0
>>>> }}}
>>>>
>>>> I should get  "1". I stared at this code for too long already.
>>>
>>> What are you trying to accomplish? In this case it could probably be
>>> done an easier way because the Integer class has lots of hacks to
>>> make it faster which may break extending it with a normal Python
>>> class.
>>>
>>> - Robert
>>
>> Hi Robert,
>>
>> I just noticed this evil property of Sage integers, i.e., they are
>> mutable
>> no matter what:
>>
>> sage: n = 5
>> sage: n.__init__(123)
>> sage: n
>> 123
>>
>> Abused this could easily lead to very subtle bugs, like the
>> one below.  Thoughts?
>>
>>
>> sage: m = matrix(ZZ,2,[1,2,3,4])
>> sage: d = m.det(); d
>> -2
>> sage: d.__init__(389)
>> sage: m
>>
>> [1 2]
>> [3 4]
>> sage: m.det()
>> 389
>>
>> The builtin Python integer type does *not* work this way:
>>
>> sage: n = int(5)
>> sage: n.__init__(123)
>> sage: n
>> 5
>>
>> Bug?
>
> Oh, that's bad... Of course, there's no stopping there:
>
> sage: K.<a> = NumberField(x^2-2)
> sage: a.__init__(K, 1/3)
> sage: a
> 1/3
>
> For Integers, I guess we could put more in the __new__ method, but I
> think in general it's just something we're going to have to accept.

Damn, I was really hoping you were not going to say that.
Every cython class has this problem, by the way, as does
every pure Python class for that matter:

sage: class foo:
...       def __init__(self, a):
...           self.b = a
sage: f = foo(10)
sage: f.b
10
sage: f.__init__(20)
sage: f.b
20

Well Sage hasn't exploded already because of this, so
I guess we'll just have to live with it.

 -- William

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to