Python如何使用vars返回对象的属性列表
英文文档:
vars([object])
Returnthe__dict__attributeforamodule,class,instance,oranyotherobjectwitha__dict__attribute.
Objectssuchasmodulesandinstanceshaveanupdateable__dict__attribute;however,otherobjectsmayhavewriterestrictionsontheir__dict__attributes(forexample,classesuseadictproxytopreventdirectdictionaryupdates).
Withoutanargument,vars()actslikelocals().Note,thelocalsdictionaryisonlyusefulforreadssinceupdatestothelocalsdictionaryareignored.
返回当前作用域内的局部变量和其值组成的字典,或者返回对象的属性列表
说明
1.当函数不接收参数时,其功能和locals函数一样,返回当前作用域内的局部变量。
#不带参数功能和locals函数一样 >>>v1=vars() >>>l1=locals() >>>v1 {'__name__':'__main__','__builtins__':,'v1':{...},'l1':{...},'__spec__':None,'__doc__':None,'__package__':None,'__loader__': } >>>l1 {'__name__':'__main__','__builtins__': ,'v1':{...},'l1':{...},'__spec__':None,'__doc__':None,'__package__':None,'__loader__': }
2.当函数接收一个参数时,参数可以是模块、类、类实例,或者定义了__dict__属性的对象。
#作用于模块 >>>importtime >>>vars(time) {'gmtime':,'tzname':('Öйú±ê׼ʱ¼ä','ÖйúÏÄÁîʱ'),'timezone':-28800,'struct_time': ,'ctime': ,'perf_counter': ,'mktime': ,'localtime': ,'time': ,'__package__':'','altzone':-32400,'clock': ,'strptime': ,'monotonic': ,'__loader__': ,'get_clock_info': ,'sleep': ,'process_time': ,'__name__':'time','_STRUCT_TM_ITEMS':9,'__spec__':ModuleSpec(name='time',loader= ,origin='built-in'),'__doc__':'Thismoduleprovidesvariousfunctionstomanipulatetimevalues.\n\nTherearetwostandardrepresentationsoftime.Oneisthenumber\nofsecondssincetheEpoch,inUTC(a.k.a.GMT).Itmaybeaninteger\norafloatingpointnumber(torepresentfractionsofseconds).\nTheEpochissystem-defined;onUnix,itisgenerallyJanuary1st,1970.\nTheactualvaluecanberetrievedbycallinggmtime(0).\n\nTheotherrepresentationisatupleof9integersgivinglocaltime.\nThetupleitemsare:\nyear(includingcentury,e.g.1998)\nmonth(1-12)\nday(1-31)\nhours(0-23)\nminutes(0-59)\nseconds(0-59)\nweekday(0-6,Mondayis0)\nJulianday(dayintheyear,1-366)\nDST(DaylightSavingsTime)flag(-1,0or1)\nIftheDSTflagis0,thetimeisgivenintheregulartimezone;\nifitis1,thetimeisgivenintheDSTtimezone;\nifitis-1,mktime()shouldguessbasedonthedateandtime.\n\nVariables:\n\ntimezone--differenceinsecondsbetweenUTCandlocalstandardtime\naltzone--differenceinsecondsbetweenUTCandlocalDSTtime\ndaylight--whetherlocaltimeshouldreflectDST\ntzname--tupleof(standardtimezonename,DSTtimezonename)\n\nFunctions:\n\ntime()--returncurrenttimeinsecondssincetheEpochasafloat\nclock()--returnCPUtimesinceprocessstartasafloat\nsleep()--delayforanumberofsecondsgivenasafloat\ngmtime()--convertsecondssinceEpochtoUTCtuple\nlocaltime()--convertsecondssinceEpochtolocaltimetuple\nasctime()--converttimetupletostring\nctime()--converttimeinsecondstostring\nmktime()--convertlocaltimetupletosecondssinceEpoch\nstrftime()--converttimetupletostringaccordingtoformatspecification\nstrptime()--parsestringtotimetupleaccordingtoformatspecification\ntzset()--changethelocaltimezone','strftime': ,'asctime': ,'daylight':0} #作用于类 >>>vars(slice) mappingproxy({'__ne__': ,'__getattribute__': ,'__reduce__': ,'start': ,'indices': ,'__ge__': ,'stop': ,'__eq__': ,'step': ,'__hash__':None,'__doc__':'slice(stop)\nslice(start,stop[,step])\n\nCreateasliceobject.Thisisusedforextendedslicing(e.g.a[0:10:2]).','__repr__': ,'__le__': ,'__gt__': ,'__new__': ,'__lt__': }) #作用于类实例 >>>classA(object): pass >>>a.__dict__ {} >>>vars(a) {} >>>a.name='Kim' >>>a.__dict__ {'name':'Kim'} >>>vars(a) {'name':'Kim'}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。