Python3与Python2中print的用法改变与Class获取属性学习

来源:互联网 发布:淘宝登录界面无法打开 编辑:程序博客网 时间:2024/06/08 14:19

Reference: http://planet.python.org/

python2中 print 'hello world'

python3中 print ('hello world')


获取属性的方法使用__dict__

import inspectclass Test:        """"""        #--------------------------------------------        def __init__(self):                self.varOne = ""                self.varTwo = ""                self.varThree = ""        #--------------------------------------------        def methodOne(self):                """"""        #       print "You just called methodOne!"                print ("You just called methodOne!")#----------------------------------------------------if __name__ == "__main__":        t = Test()        variables = [i for i in t.__dict__ if not inspect.ismethod(i)]        print (variables)        print ('End of File')
以下两种方法也可以,并且第二种不需要import inspect.

variables = [i for i in t.__dict__ if not callable(i)]

variables = t.__dict__.keys()