instance returns False after modifying file

10 views
Skip to first unread message

tvn

unread,
Mar 24, 2012, 2:31:12 PM3/24/12
to sage-s...@googlegroups.com
This seems to be a very weird behavior.  After modifying a file,  all instantiated instances V of classes C in the file are no longer correct (i.e. instance(V,C)  returns false).  

Here's a simple example illustrating the problem 

I have a file called t1.py contains the following
class Test(object):
    def __init__(self):
        pass


Now open sage, at the prompt do

$ sage
----------------------------------------------------------------------
| Sage Version 4.8, Release Date: 2012-01-20                         |
| Type notebook() for the GUI, and license() for information.        |
----------------------------------------------------------------------
sage: attach t1.py
sage: t = Test()
sage: isinstance(t,Test)
True         #### here it's correct


Now go back to the file t1.py,  make any modification ,  e.g.  enter a space and delete it ... save the file

sage: isinstance(t,Test)
False   ####  here it's no longer correct


Justin C. Walker

unread,
Mar 24, 2012, 2:46:02 PM3/24/12
to sage-s...@googlegroups.com

On Mar 24, 2012, at 11:31 , tvn wrote:

> This seems to be a very weird behavior. After modifying a file, all
> instantiated instances V of classes C in the file are no longer correct
> (i.e. instance(V,C) returns false).
>
> Here's a simple example illustrating the problem

[snip]


> Now go back to the file t1.py, make any modification , e.g. enter a
> space and delete it ... save the file
>
> sage: isinstance(t,Test)
> False #### here it's no longer correct

While it seems wrong/weird, I think it actually makes sense. The root of problem is that the explanation lies in the implementation details. The following is a rough sketch; the details are, well, details :-}

To test whether something is an instance of a type, you have to have a specification for the type. When you modified the file that you previously attached, it was reloaded, and that action removed the old version and added the new version. The way types are maintained internally causes the "isinstance()" check to fail because, in fact, the old instance is not of the new type.

Keep in mind that the internal code is not going to try to figure out that a newly-loaded version is different from the previous version; it's just "out with the old; in with the new".

When I'm in the middle of the "Edit/Reload/Debug" loop, I have to make sure I redefine all the gizmos associated with the files I'm editing. Otherwise, strange and wondrous things happen.

If I have this wrong, I think someone will speak up, but that's Truth as I understand it.

HTH

Justin

--
Justin C. Walker, Curmudgeon-At-Large
Institute for the Enhancement of the Director's Income
--------
When LuteFisk is outlawed,
Only outlaws will have LuteFisk
--------

William Stein

unread,
Mar 24, 2012, 2:48:35 PM3/24/12
to sage-s...@googlegroups.com


On Mar 24, 2012 2:46 PM, "Justin C. Walker" <jus...@mac.com> wrote:
>
>
> On Mar 24, 2012, at 11:31 , tvn wrote:
>
> > This seems to be a very weird behavior.  After modifying a file,  all
> > instantiated instances V of classes C in the file are no longer correct
> > (i.e. instance(V,C)  returns false).
> >
> > Here's a simple example illustrating the problem
> [snip]
> > Now go back to the file t1.py,  make any modification ,  e.g.  enter a
> > space and delete it ... save the file
> >
> > sage: isinstance(t,Test)
> > False   ####  here it's no longer correct
>
> While it seems wrong/weird, I think it actually makes sense.  The root of problem is that the explanation lies in the implementation details.  The following is a rough sketch; the details are, well, details :-}
>

Good explanation.

The OP might consider using:

import t1
... do stuff ...
reload(t1)

The above is l standard Python.

  - william

> To test whether something is an instance of a type, you have to have a specification for the type.  When you modified the file that you previously attached, it was reloaded, and that action removed the old version and added the new version.  The way types are maintained internally causes the "isinstance()" check to fail because, in fact, the old instance is not of the new type.
>
> Keep in mind that the internal code is not going to try to figure out that a newly-loaded version is different from the previous version; it's just "out with the old; in with the new".
>
> When I'm in the middle of the "Edit/Reload/Debug" loop, I have to make sure I redefine all the gizmos associated with the files I'm editing.  Otherwise, strange and wondrous things happen.
>
> If I have this wrong, I think someone will speak up, but that's Truth as I understand it.
>
> HTH
>
> Justin
>
> --
> Justin C. Walker, Curmudgeon-At-Large
> Institute for the Enhancement of the Director's Income
> --------
> When LuteFisk is outlawed,
> Only outlaws will have LuteFisk
> --------
>
>
>

> --
> To post to this group, send email to sage-s...@googlegroups.com
> To unsubscribe from this group, send email to sage-support...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/sage-support
> URL: http://www.sagemath.org

Reply all
Reply to author
Forward
0 new messages