Python dir() function with Examples - Javatpoint
文章推薦指數: 80 %
Python dir() function returns the list of names in the current local scope. If the object on which method is called has a method named __dir__(), ... ⇧SCROLLTOTOP Home Python IfElse ForLoop Function Array String Regex List Set Tuple Dictionary Programs Numpy InterviewQuestions PythonFunctions PythonBuilt-inFunctions Pythonabs() Pythonall() Pythonbin() Pythonbool() Pythonbytes() Pythoncallable() Pythoncompile() Pythonexec() Pythonsum() Pythonany() Pythonascii() Pythonbytearray() Pythoneval() Pythonfloat() Pythonformat() Pythonfrozenset() Pythongetattr() Pythonglobals() Pythonhasattr() Pythoniter() Pythonlen() Pythonlist() Pythonlocals() Pythonmap() Pythonmemoryview() Pythonobject() Pythonopen() Pythonord() Pythonpow() Pythonprint() Pythonreversed() Pythonrange() Pythonround() Pythonstr() Pythontuple() Pythontype() Pythonvars() Pythonzip() Pythonchr() Pythoncomplex() Pythondelattr() Pythondir() Pythondivmod() Pythonenumerate() Pythondict() Pythonfilter() Pythonhash() Pythonhelp() Pythonmin() Pythonset() Pythonhex() Pythonid() Pythonsetattr() Pythonslice() Pythonsorted() Pythonnext() Pythoninput() Pythonint() Pythonisinstance() Pythonoct() next→ ←prev Pythondir()Function Pythondir()functionreturnsthelistofnamesinthecurrentlocalscope.Iftheobjectonwhichmethodiscalledhasamethodnamed__dir__(),thismethodwillbecalledandmustreturnthelistofattributes.Ittakesasingleobjecttypeargument.Thesignatureofthefunctionisgivenbelow. Signature dir([object]) Parameters object:Ittakesanoptionalparameter. Return Itreturnsalistofvalidattributesoftheobject. Let'sseesomeexamplesofdir()functiontounderstandit'sfunctionality. Pythondir()FunctionExample1 Let'screateasimpleexampletogetalistofvalidattributes.Ittakesasingleparameterwhichisoptional. #Pythondir()functionexample #Callingfunction att=dir() #Displayingresult print(att) Output: ['__annotations__','__builtins__','__cached__','__doc__','__file__','__loader__','__name__','__package__', '__spec__'] Pythondir()FunctionExample2 Ifwepassaparametertothisfunction,itreturnsattributesrelatedtothatobject.Seeanexamplebelow. #Pythondir()functionexample lang=("C","C++","Java","Python") #Callingfunction att=dir(lang) #Displayingresult print(att) Output: ['__add__','__class__','__contains__','__delattr__','__dir__','__doc__','__eq__','__format__','__ge__','__getattribute__', '__getitem__','__getnewargs__','__gt__','__hash__','__init__','__iter__','__le__','__len__','__lt__','__mul__', '__ne__','__new__','__reduce__','__reduce_ex__','__repr__','__rmul__','__setattr__','__sizeof__','__str__', '__subclasshook__','count','index'] Pythondir()FunctionExample3 #Pythondir()functionexample classStudent(): def__init__(self,x): returnself.x #Callingfunction att=dir(Student) #Displayingresult print(att) Output: ['__class__','__delattr__','__dict__','__dir__','__doc__','__eq__','__format__','__ge__','__getattribute__','__gt__','__hash__','__init__','__le__','__lt__','__module__','__ne__','__new__','__reduce__','__reduce_ex__','__repr__','__setattr__','__sizeof__','__str__','__subclasshook__','__weakref__'] Pythondir()FunctionExample4 Ifdir()functionisalreadydefinedintheobject,thefunctionwillbecalled. #Pythondir()functionexample classStudent(): def__dir__(self): return[10,20,30] #Callingfunction s=Student() att=dir(s) #Displayingresult print(att) Output: [10,20,30] NextTopicPythonFunctions ←prev next→ ForVideosJoinOurYoutubeChannel:JoinNow Feedback SendyourFeedbackto[email protected] HelpOthers,PleaseShare LearnLatestTutorials Splunk SPSS Swagger Transact-SQL Tumblr ReactJS Regex ReinforcementLearning RProgramming RxJS ReactNative PythonDesignPatterns PythonPillow PythonTurtle Keras Preparation Aptitude Reasoning VerbalAbility InterviewQuestions CompanyQuestions TrendingTechnologies ArtificialIntelligence AWS Selenium CloudComputing Hadoop ReactJS DataScience Angular7 Blockchain Git MachineLearning DevOps B.Tech/MCA DBMS DataStructures DAA OperatingSystem ComputerNetwork CompilerDesign ComputerOrganization DiscreteMathematics EthicalHacking ComputerGraphics SoftwareEngineering WebTechnology CyberSecurity Automata CProgramming C++ Java .Net Python Programs ControlSystem DataMining DataWarehouse JavatpointServicesJavaTpointofferstoomanyhighqualityservices.Mailuson[email protected],togetmoreinformationaboutgivenservices.WebsiteDesigningWebsiteDevelopmentJavaDevelopmentPHPDevelopmentWordPressGraphicDesigningLogoDigitalMarketingOnPageandOffPageSEOPPCContentDevelopmentCorporateTrainingClassroomandOnlineTrainingDataEntryTrainingForCollegeCampusJavaTpointofferscollegecampustrainingonCoreJava,AdvanceJava,.Net,Android,Hadoop,PHP,WebTechnologyandPython.Pleasemailyourrequirementat[email protected]Duration:1weekto2weekLike/Subscribeusforlatestupdatesornewsletter
延伸文章資訊
- 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...
- 2Python dir() - ItsMyCode
The dir() method is an inbuilt function in Python which returns all the valid list of attributes ...
- 3dir() function in Python - GeeksforGeeks
dir() is a powerful inbuilt function in Python3, which returns list of the attributes and methods...
- 4dir() Method in Python - Tutorialspoint
When we print the value of the dir() without importing any other module into the program, we get ...
- 5Built-in Functions — Python 3.10.4 documentation
import struct >>> dir() # show the names in the module namespace ... Since printed arguments are ...