All pastes #2071774 Raw Edit

Stuff

public text v1 · immutable
#2071774 ·published 2011-05-30 01:04 UTC
rendered paste body
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) 
[GCC 4.5.2] on linux2
Type "copyright", "credits" or "license()" for more information.
DreamPie 1.1
>>> class Point:
...     def __init__(self,x,y):
...         self.x, self.y = x,y
...     def __hash__(self):
...         return hash((x,y))
>>> d = {}
>>> p = Point(1,2)
>>> d[p] = 5
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    d[p] = 5
  File "<pyshell#0>", line 5, in __hash__
    return hash((x,y))
NameError: global name 'x' is not defined
>>> class Point:
...     def __init__(self,x,y):
...         self.x, self.y = x,y
...     def __hash__(self):
...         return hash((self.x,self.y))
...     def __eq__(self,other):
...         if self.x == other.x and self.y == other.y:
...             return True
...         else:
...             return False
>>> p = Point(1,2)
>>> d[p] = 5
>>> d
0: {<__main__.Point instance at 0xcd32d8>: 5}
>>> p.x = 64
>>> d
1: {<__main__.Point instance at 0xcd32d8>: 5}
>>> d[p]
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    d[p]
KeyError: <__main__.Point instance at 0xcd32d8>
>>> d[Point(1,2)]
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    d[Point(1,2)]
KeyError: <__main__.Point instance at 0xcd34d0>
>>> class Point:
...     def __init__(self,x,y):
...         self.x, self.y = x,y
...     def __hash__(self):
...         return hash((self.x,self.y))
...     def __eq__(self,other):
...         if self.x == other.x and self.y == other.y:
...             return True
...         else:
...             return False
...     def __repr__(self):
...         return "Point({},{})".format(self.x,self.y)
>>> del d
>>> del p
>>> p = Point(1,2)
>>> d = {}
>>> d[p] = 500
>>> d
2: {Point(1,2): 500}
>>> p.y = 500
>>> d[p]
Traceback (most recent call last):
  File "<pyshell#20>", line 1, in <module>
    d[p]
KeyError: Point(1,500)
>>> d[Point(1,2)]
Traceback (most recent call last):
  File "<pyshell#21>", line 1, in <module>
    d[Point(1,2)]
KeyError: Point(1,2)
>>> del p
>>> d[Point(1,2)]
Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    d[Point(1,2)]
KeyError: Point(1,2)
>>> p
Traceback (most recent call last):
  File "<pyshell#24>", line 1, in <module>
    p
NameError: name 'p' is not defined
>>> d
3: {Point(1,500): 500}
>>> d[Point(1,500)]
Traceback (most recent call last):
  File "<pyshell#26>", line 1, in <module>
    d[Point(1,500)]
KeyError: Point(1,500)
>>> d.keys()
4: [Point(1,500)]
>>> d.keys()[0]
5: Point(1,500)
>>> d.keys()[0] == Point(1,500)
6: True
>>> d.keys()[0].y = 2
>>> d
7: {Point(1,2): 500}
>>> d[Point(1,2)]
8: 500
>>>