Python内置函数—vars的具体使用方法
本文文章主要介绍了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.
参数
object--对象
返回值
返回对象object的属性和属性值的字典对象,如果没有参数,就打印当前调用位置的属性和属性值类似locals()。
说明
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'}
本函数是实现返回对象object的属性和属性值的字典对象。如果默认不输入参数,就打印当前调用位置的属性和属性值,相当于locals()的功能。如果有参数输入,就只打印这个参数相应的属性和属性值。
例子:
#vars() print(vars()) classFoo: a=1 print(vars(Foo)) foo=Foo() print(vars(foo))
结果输出如下:
{'Foo':,'B': ,'__name__':'__main__','__doc__':None,'__spec__':None,... {'__module__':'__main__','a':1,'__dict__': ,'__weakref__': ,'__doc__':None} {}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。