An interesting python problem

Johnny Lee johnnyandfiona at hotmail.com
Wed Sep 14 04:16:13 EDT 2005


Hi,
   Look at the follow command in python command line, See what's
interesting?:)

>>> class A:
	i = 0
>>> a = A()
>>> b = A()
>>> a.i = 1
>>> print a.i, b.i
1 0

---------------------------------------

>>> class A:
	arr = []
>>> a = A()
>>> b = A()
>>> a
<__main__.A instance at 0x00C96698>
>>> b
<__main__.A instance at 0x00CA0760>
>>> A
<class __main__.A at 0x00B314B0>
>>> a.arr.append("haha")
>>> print a.arr , b.arr
['haha'] ['haha']
>>> a.arr = ["xixi"]
>>> print a.arr , b.arr
['xixi'] ['haha']
>>> A.arr
['haha']
>>> A.arr.append("xx")
>>> A.arr
['haha', 'xx']
>>> a.arr
['xixi']
>>> b.arr
['haha', 'xx']
>>> b.arr.pop()
'xx'
>>> b.arr
['haha']
>>> A.arr
['haha']

-------------------------------------

>>> class X:
	def __init__(self):
		self.arr = []
>>> m = X()
>>> n = X()
>>> m.arr.append("haha")
>>> print m.arr, n.arr
['haha'] []




More information about the Python-list mailing list