Python dir() - Programiz
文章推薦指數: 80 %
Python dir(). The dir() method tries to return a list of valid attributes of the object. The syntax of dir() is: dir([object]) ... CourseIndex ExploreProgramiz Python JavaScript SQL C C++ Java Kotlin Swift C# DSA LearnPythonpractically andGetCertified. ENROLL PopularTutorials GettingStartedWithPython PythonifStatement whileLoopinPython PythonLists DictionariesinPython StartLearningPython PopularExamples Addtwonumbers Checkprimenumber Findthefactorialofanumber PrinttheFibonaccisequence Checkleapyear ExplorePythonExamples ReferenceMaterials Built-inFunctions ListMethods DictionaryMethods StringMethods Viewall LearningPaths Challenges LearnPythonInteractively TryforFree Courses BecomeaPythonMaster BecomeaCMaster ViewallCourses Python JavaScript SQL C C++ Java Kotlin Swift C# DSA LearnPythonpractically andGetCertified. ENROLLFORFREE! PopularTutorials GettingStartedWithPython PythonifStatement whileLoopinPython PythonLists DictionariesinPython StartLearningPython AllPythonTutorials ReferenceMaterials Built-inFunctions ListMethods DictionaryMethods StringMethods Viewall Python JavaScript C C++ Java Kotlin LearnPythonpractically andGetCertified. ENROLLFORFREE! PopularExamples Addtwonumbers Checkprimenumber Findthefactorialofanumber PrinttheFibonaccisequence Checkleapyear AllPythonExamples Built-inFunctions Pythonabs() Pythonany() Pythonall() Pythonascii() Pythonbin() Pythonbool() Pythonbytearray() Pythoncallable() Pythonbytes() Pythonchr() Pythoncompile() Pythonclassmethod() Pythoncomplex() Pythondelattr() Pythondict() Pythondir() Pythondivmod() Pythonenumerate() Pythonstaticmethod() Pythonfilter() Pythoneval() Pythonfloat() Pythonformat() Pythonfrozenset() Pythongetattr() Pythonglobals() Pythonexec() Pythonhasattr() Pythonhelp() Pythonhex() Pythonhash() Pythoninput() Pythonid() Pythonisinstance() Pythonint() Pythonissubclass() Pythoniter() Pythonlist()Function Pythonlocals() Pythonlen() Pythonmax() Pythonmin() Pythonmap() Pythonnext() Pythonmemoryview() Pythonobject() Pythonoct() Pythonord() Pythonopen() Pythonpow() Pythonprint() Pythonproperty() Pythonrange() Pythonrepr() Pythonreversed() Pythonround() Pythonset() Pythonsetattr() Pythonslice() Pythonsorted() Pythonstr() Pythonsum() Pythontuple()Function Pythontype() Pythonvars() Pythonzip() Python__import__() Pythonsuper() RelatedTopics Pythonobject() PythonListsVsTuples Pythontype() Pythonvars() PythonObjectsandClasses PythonObjectOrientedProgramming Pythondir() Thedir()methodtriestoreturnalistofvalidattributesoftheobject. Thesyntaxofdir()is: dir([object]) dir()Parameters dir()takesmaximumofoneobject. object(optional)-dir()attemptstoreturnallattributesofthisobject. ReturnValuefromdir() dir()triestoreturnalistofvalidattributesoftheobject. Iftheobjecthas__dir__()method,themethodwillbecalledandmustreturnthelistofattributes. Iftheobjectdoesn'thave__dir__()method,thismethodtriestofindinformationfromthe__dict__attribute(ifdefined),andfromtypeobject.Inthiscase,thelistreturnedfromdir()maynotbecomplete. Ifanobjectisnotpassedtodir()method,itreturnsthelistofnamesinthecurrentlocalscope. Example1:Howdir()works? number=[1,2,3] print(dir(number)) print('\nReturnValuefromemptydir()') print(dir()) Output ['__add__','__class__','__contains__','__delattr__', '__delitem__','__dir__','__doc__','__eq__','__format__', '__ge__','__getattribute__','__getitem__','__gt__','__hash__', '__iadd__','__imul__','__init__','__init_subclass__', '__iter__','__le__','__len__','__lt__','__mul__','__ne__', '__new__','__reduce__','__reduce_ex__','__repr__','__reversed__', '__rmul__','__setattr__','__setitem__','__sizeof__','__str__', '__subclasshook__','append','clear','copy','count','extend', 'index','insert','pop','remove','reverse','sort'] ReturnValuefromemptydir() ['__annotations__','__builtins__','__doc__','__loader__', '__name__','__package__','__spec__','number'] Example2:dir()onUser-definedObject classPerson: def__dir__(self): return['age','name','salary'] teacher=Person() print(dir(teacher)) Output ['age','name','salary'] PreviousTutorial: Pythondict() NextTutorial: Pythondivmod() Shareon: Didyoufindthisarticlehelpful? Sorryaboutthat. Howcanweimproveit? Feedback* Leavethisfieldblank PythonReferencesPythonLibraryPythonobject()PythonLibraryPythonlist()PythonLibraryPythontype()PythonLibraryPythonprint() GetApp GetPythonApp
延伸文章資訊
- 1How to Use dir() Function In Python - AppDividend
Python dir() is an inbuilt method that returns a list of the attributes and methods of any object...
- 2Built-in Functions — Python 3.10.4 documentation
import struct >>> dir() # show the names in the module namespace ... Since printed arguments are ...
- 3Python dir() Function - W3Schools
The dir() function returns all properties and methods of the specified object, without the values...
- 4Python dir() function with Examples - Javatpoint
Python dir() function returns the list of names in the current local scope. If the object on whic...
- 5Python基礎功不可少-dir()與help()的使用
Help on built-in function dir in module builtins: dir(…) dir([object]) -> list of strings If call...