dir() Method in Python - Tutorialspoint
文章推薦指數: 80 %
When we print the value of the dir() without importing any other module into the program, we get the list of methods and attributes that are ... TrendingCategories DataStructure Networking RDBMS OperatingSystem Java iOS HTML CSS Android Python CProgramming C++ C# MongoDB MySQL Javascript PHP SelectedReading UPSCIASExamsNotes Developer'sBestPractices QuestionsandAnswers EffectiveResumeWriting HRInterviewQuestions ComputerGlossary WhoisWho dir()MethodinPython PythonServerSideProgrammingProgramming Thedir()functionreturnslistoftheattributesandmethodsofanyobjectlikefunctions,modules,strings,lists,dictionariesetc.Inthisarticlewewillseehowtousethedir()indifferentwaysinaprogramandfordifferentrequirements.Onlydir()Whenweprintthevalueofthedir()withoutimportinganyothermoduleintotheprogram,wegetthelistofmethodsandattributesthatareavailableaspartofthestandardlibrarythatisavailablewhenapythonprogramisinitialized.ExamplePrint(dir())OutputRunningtheabovecodegivesusthefollowingresult−['__annotations__','__builtins__','__cached__','__doc__','__file__','__loader__','__name__','__package__','__spec__']AdditionalmodulesAsweimportadditionalmodulesandcreatevariables,theygetaddedtothecurrentenvironment.Thenthosemethodsandattributesalsobecomeavailableintheprintstatementwothdir().Exampleimportmath x=math.ceil(10.03) print(dir())OutputRunningtheabovecodegivesusthefollowingresult−['__annotations__','__builtins__','__cached__','__doc__','__file__','__loader__','__name__','__package__','__spec__','math','x']dir()forspecificModulesForspecificmoduleswecanfindthemethodsandattributescontainedinthatmodulebypassingitasparametertothedir().Inthebelowexampleweseethemethodsavailableinthemathmodule.Exampleimportmath print(dir(math))OutputRunningtheabovecodegivesusthefollowingresult−['__doc__','__loader__','__name__','__package__','__spec__','acos','acosh','asin','asinh','atan','atan2','atanh','ceil','copysign',….,'nan',…'trunc']dir()foraclassWecanalsoapplythedir()toaclasswhichwasusercreatedratherthanin-bulitandgetsitsattributeslistedthroughdir().Example LiveDemoclassmoviecount: def__dir__(self): return['RedMan','HelloBoy','HappyMonday'] movie_dtls=moviecount() print(dir(movie_dtls))OutputRunningtheabovecodegivesusthefollowingresult−['HappyMonday','HelloBoy','RedMan'] PradeepElance Publishedon26-Aug-202007:03:18 RelatedQuestions&AnswersThedir()FunctioninPython HTMLdirAttribute HTMLDOMdirproperty HTMLDOMBdodirProperty Whatisthedifferencebetweendir(),globals()andlocals()functionsinPython? ClassmethodvsstaticmethodinPython evalmethodinPython? numpy.vanderMethodinPython numpy.matrixMethodinPython numpy.trilMethodinPython numpy.triuMethodinPython numpy.triMethodinPython Python-getattr()method Python-cmp()Method Python-iter()method PreviousPage PrintPage NextPage Advertisements Print AddNotes Bookmarkthispage ReportError Suggestions Save Close Dashboard Logout
延伸文章資訊
- 1dir() function in Python - GeeksforGeeks
dir() is a powerful inbuilt function in Python3, which returns list of the attributes and methods...
- 2Python dir() - Programiz
Python dir(). The dir() method tries to return a list of valid attributes of the object. The synt...
- 3dir() Method in Python - Tutorialspoint
When we print the value of the dir() without importing any other module into the program, we get ...
- 4Python基礎功不可少-dir()與help()的使用
Help on built-in function dir in module builtins: dir(…) dir([object]) -> list of strings If call...
- 5Python dir() Function - W3Schools
The dir() function returns all properties and methods of the specified object, without the values...