Python3 Func Dir
## Python3.x Python dir() Function\\n\\n[ Python3 Built-in Functions](#)\\n\\n* * *\\n\\n`dir()` is a built-in function in Python used to get a list of an object's attributes and methods.\\n\\n`dir()` returns a list containing the names of all attributes and methods of an object. This function is very useful for exploring unknown objects, debugging code, and understanding class structures.\\n\\n**Word Definition**: `dir` is an abbreviation for `directory`, referring here to a list of attributes and methods.\\n\\n* * *\\n\\n## Basic Syntax and Parameters\\n\\n### Syntax Format\\n\\ndir(object) dir()\\n### Parameter Description\\n\\n* **Parameter object** (optional):\\n * Type: Any object\\n * Description: The object whose attribute list is to be retrieved. If no parameter is passed, it returns the list of names in the current scope.\\n\\n### Function Description\\n\\n* **Return Value**: Returns a list of strings containing the names of attributes and methods.\\n* **Feature**: The list is sorted in alphabetical order.\\n\\n* * *\\n\\n## Examples\\n\\n### Example 1: Viewing an Object's Attributes\\n\\n## Example\\n\\n# View all attributes and methods of a list\\n\\nprint(dir([]))\\n\\n# Output: ['__add__', '__class__', '__contains__', ... 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']\\n\\n# View string attributes\\n\\nprint(dir(""))\\n\\n# Output: ['__add__', '__class__', '__contains__', ... 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']\\n\\n# View dictionary attributes\\n\\nprint(dir({}))\\n\\n# Output: ['__class__', '__contains__', ... 'clear', 'copy', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']\\n\\n**Expected Output:**\\n\\nReturns a list of all attribute and method names of the object.\\n\\n**Code Analysis:**\\n\\n1. Those starting with double underscores `__` are special attributes/methods (magic methods).\\n2. Those without double underscores are public methods, such as `append`, `sort`, etc.\\n\\n### Example 2: Viewing Attributes of a Custom Class\\n\\n## Example\\n\\nclass Person:\\n\\ndef __init__ (self, name, age):\\n\\nself.name= name\\n\\nself.age= age\\n\\ndef greet(self):\\n\\nreturn f"Hello, I'm {self.name}"\\n\\n# Create instance\\n\\n p = Person("Tom",20)\\n\\n# View instance attributes\\n\\nprint(dir(p))\\n\\n# Output: ['__class__', '__delattr__', '__dict__', '__init__', ... 'age', 'greet', 'name']\\n\\n# Filter to show only non-special methods\\n\\n attrs =[attr for attr in dir(p)if not attr.startswith('_')]\\n\\nprint(attrs)# Output: ['age', 'greet', 'name']\\n\\n**Expected Output:**\\n\\n['__class__', '__delattr__', '__dict__', '__init__', ... 'age', 'greet', 'name']['age', 'greet', 'name']\\n**Code Analysis:**\\n\\n* `__dict__` contains the instance attributes of the object.\\n* Filtering out methods starting with `__` yields the public attributes.\\n\\n### Example 3: Using Without Parameters\\n\\n## Example\\n\\n# Without arguments, returns a list of names in the current scope\\n\\nprint(dir())\\n\\n# Output: ['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']\\n\\n# View after importing module\\n\\nimport os\\n\\nprint(dir())\\n\\n# Will contain 'os' In the list\\n\\n# View module attributes\\n\\nimport json\\n\\nprint(dir(json))\\n\\n# Output: ['JSONDecodeError', '__all__', '__author__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'decoder', 'dump', 'dumps', 'encoder', 'load', 'loads', 'tool']\\n\\n**Expected Output:**\\n\\nReturns a list of available names in the current scope.\\n\\n`dir()` is a powerful tool for exploring Python objects and modules, especially suited for interactive learning and debugging.\\n\\n* * Python3 Built-in Functions](#)\\n\\n* * *
YouTip