首页 > 编程语言 > 详细

Python 类与作用域——一些测试

时间:2015-06-01 00:36:23      阅读:300      评论:0      收藏:0      [点我收藏+]

 

/* 一 */
>>> class T ():
        a = 0
        b = a
    
>>> T.b
0
>>> del T


/* 二 */
>>> class T ():
        a = 0
        b = T.a

Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    class T ():
  File "<pyshell#9>", line 3, in T
    b = T.a
NameError: name T is not defined


/* 三 */
>>> class T ():
        def a (self):
            print (a)
        def ab (self):
            a (self)
            print (b)

>>> T.ab (test)
Traceback (most recent call last):
  File "<pyshell#19>", line 1, in <module>
    T.ab (test)
  File "<pyshell#18>", line 5, in ab
    a (self)
NameError: name a is not defined
>>> del T


/* 四 */
>>> class T ():
        def a (self):
            print (a)
        def ab (self):
            T.a (self)
            print (b)
        
>>> T.ab (test)
a
b
>>> del T


/* 五 */
>>> class T ():
        a = 0
        def pa (self):
            print (a)
    
>>> T.pa (test)
Traceback (most recent call last):
  File "<pyshell#27>", line 1, in <module>
    T.pa (test)
  File "<pyshell#25>", line 4, in pa
    print (a)
NameError: name a is not defined
>>> del T


/* 六 */
>>> class T ():
    a = 0
    def pa (self):
        print (T.a)

>>> T.pa (test)
0
>>> del T
>>> 


--date-- 2015-05-31

Python 类与作用域——一些测试

原文:http://www.cnblogs.com/pycansi/p/4542977.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!