Re: A strange behavior for isinstance method

"[email protected]" <[email protected]>
Newsgroups gmane.comp.lang.jython.user
Message-ID <[email protected]>
Dear Jeff,

Excellent explaination! Thanks a lot!

By the way, as a China user, my GMAIL system is so instable in rencent days, so I have to use another email address.

Regards
Yaqiang


*********************************************** forward
Happy New Year from 2014.

From the docs for reload():
When a module is reloaded, its dictionary (containing the module's global variables) is retained.

If a module instantiates instances of a class, reloading the module that defines the class does not affect the method definitions of the instances ― they continue to use the old class definition.

When you reload a module you execute its body afresh. In your case, this means that point.Point is redefined to reference a new class object. But in your main program you have retained a reference "Point" to the old class object and p1, p2, p3 are instances of the old class. So when you call __add__ by p2 + p2, you call the add method of the old class.

However, within that class, when make reference to "Point", Python looks up the meaning of the symbol in that module's dictionary. This dictionary has been refilled by the reload, so it finds the new class called Point. Tricky, eh?

If you rewrite as below, you can see it happen between the second and third calls.

Jeff

# point.py
# ---------
class Point():
    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y

    def __str__(self):
        return "({0},{1})".format(self.x, self.y) 

    def __add__(self, other):
        print 'Inside __add__:'
        print '  Type test (Point): ' + str(isinstance(other, Point)) 
        print '  Type test (self) : ' + str(isinstance(other, self.__class__)) 
        x = self.x + other.x 
        y = self.y + other.y 
        return Point(x, y) 


#Run test in module
print '-------------Test inside module ---------------'
print 'In point module:'
p1 = Point(2,3)
p2 = Point(-1,2)
print '  Type test (Point): '+ str(isinstance(p2, Point))
print '  Type test (p1): '+ str(isinstance(p2, p1.__class__))
p3 = p1 + p2
print 

# test_type.py
# -------------
import point
from point import Point

reload(point)

#Run test
print '-------------Test outside module ---------------'
print 'In __main__ module:'
p1 = Point(2,3)
p2 = Point(-1,2)
print '  Type test (Point): '+ str(isinstance(p2, Point))
print '  Type test (p1)   : '+ str(isinstance(p2, p1.__class__))
p3 = p1 + p2

-------------Test inside module ---------------
In point module:
  Type test (Point): True
  Type test (p1): True
Inside __add__:
  Type test (Point): True
  Type test (self) : True

-------------Test inside module ---------------
In point module:
  Type test (Point): True
  Type test (p1): True
Inside __add__:
  Type test (Point): True
  Type test (self) : True

-------------Test outside module ---------------
In __main__ module:
  Type test (Point): True
  Type test (p1)   : True
Inside __add__:
  Type test (Point): False
  Type test (self) : True
Jeff Allen



**************************************************
Dr. Yaqiang Wang
Chinese Academy of Meteorological Sciences (CAMS)
46, Zhong-Guan-Cun South Avenue
Beijing, 100081
China

http://www.meteothinker.com
http://hi.baidu.com/meteoinfo
            
[email protected]
***************************************************

------------------------------------------------------------------------------
Dive into the World of Parallel Programming! The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net

_______________________________________________
Jython-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jython-users
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.