18.8. Idiom Dir
dir([object])If called without an argument, return the names in the current scope
Else, return an alphabetized list of names comprising (some of) the attributes of the given object, and of attributes reachable from it
18.8.1. SetUp
>>> class User:
... def __init__(self, username, password):
... self.username = username
... self.password = password
...
... def login(self):
... return 'logged-in'
...
... def logout(self):
... return 'logged-out'
18.8.2. Class
>>> dir(User)
['__class__', '__delattr__', '__dict__', '__dir__',
'__doc__', '__eq__', '__firstlineno__', '__format__',
'__ge__', '__getattribute__', '__getstate__', '__gt__',
'__hash__', '__init__', '__init_subclass__', '__le__',
'__lt__', '__module__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
'__static_attributes__', '__str__', '__subclasshook__',
'__weakref__', 'login', 'logout']
18.8.3. Instance
>>> alice = User('alice', 'secret')
>>>
>>> dir(alice)
['__class__', '__delattr__', '__dict__', '__dir__',
'__doc__', '__eq__', '__firstlineno__', '__format__',
'__ge__', '__getattribute__', '__getstate__', '__gt__',
'__hash__', '__init__', '__init_subclass__', '__le__',
'__lt__', '__module__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
'__static_attributes__', '__str__', '__subclasshook__',
'__weakref__', 'login', 'logout', 'password', 'username']
18.8.4. Dir vs Vars
Function
vars()returns variables onlyFunction
dir()returns variables and methods
>>> alice = User('alice', 'secret')
>>>
>>> vars(alice)
{'username': 'alice', 'password': 'secret'}
>>>
>>> dir(alice)
['__class__', '__delattr__', '__dict__', '__dir__',
'__doc__', '__eq__', '__firstlineno__', '__format__',
'__ge__', '__getattribute__', '__getstate__', '__gt__',
'__hash__', '__init__', '__init_subclass__', '__le__',
'__lt__', '__module__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
'__static_attributes__', '__str__', '__subclasshook__',
'__weakref__', 'login', 'logout', 'password', 'username']
18.8.5. Public Attributes
>>> result = []
>>> for name in dir(alice):
... if not name.startswith('_'):
... result.append(name)
>>>
>>> print(result)
['login', 'logout', 'password', 'username']
18.8.6. Public Variables
for name in dir(alice)if not name.startswith('_')if name in vars(alice)
>>> result = []
>>> for name in dir(alice):
... if not name.startswith('_'):
... if name in vars(alice):
... result.append(name)
>>>
>>> print(result)
['password', 'username']
18.8.7. Public Methods
for name in dir(alice)if not name.startswith('_')if name not in vars(alice)
>>> result = []
>>> for name in dir(alice):
... if not name.startswith('_'):
... if name not in vars(alice):
... result.append(name)
>>>
>>> print(result)
['login', 'logout']
18.8.8. Assignments
# %% About
# - Name: Idiom Dir Class
# - Difficulty: easy
# - Lines: 1
# - Minutes: 1
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 1. Get list of attributes of `User` class
# 2. Define `result: list` with the result
# 3. Run doctests - all must succeed
# %% Polish
# 1. Pobierz listę atrybutów klasy `User`
# 2. Zdefiniuj `result: list` z wynikiem
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> result
# ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__',
# '__eq__', '__firstlineno__', '__format__', '__ge__', '__getattribute__',
# '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__',
# '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__',
# '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__static_attributes__',
# '__str__', '__subclasshook__', '__weakref__', 'login', 'logout']
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> from pprint import pprint
>>> from inspect import isclass
>>> assert isclass(User), \
'Object `User` has an invalid type; expected: `class`.'
>>> assert 'authenticated' not in result
>>> assert 'password' not in result
>>> assert 'username' not in result
>>> assert '__init__' in result
>>> assert 'login' in result
>>> assert 'logout' in result
>>> result # doctest: +NORMALIZE_WHITESPACE
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__',
'__eq__', '__firstlineno__', '__format__', '__ge__', '__getattribute__',
'__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__',
'__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__static_attributes__',
'__str__', '__subclasshook__', '__weakref__', 'login', 'logout']
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result: dict[str, str|bool]
# %% Data
class User:
def __init__(self, username, password):
self.username = username
self.password = password
self.authenticated = False
def login(self):
self.authenticated = True
def logout(self):
self.authenticated = False
# %% Result
result = ...
# %% About
# - Name: Idiom Dir Instance
# - Difficulty: easy
# - Lines: 1
# - Minutes: 1
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 1. Get list of attributes of `alice` instance
# 2. Define `result: list` with the result
# 3. Run doctests - all must succeed
# %% Polish
# 1. Pobierz listę atrybutów instancji `alice`
# 2. Zdefiniuj `result: list` z wynikiem
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> result
# ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__',
# '__eq__', '__firstlineno__', '__format__', '__ge__', '__getattribute__',
# '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__',
# '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__',
# '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
# '__static_attributes__', '__str__', '__subclasshook__', '__weakref__',
# 'authenticated', 'login', 'logout', 'password', 'username']
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> from pprint import pprint
>>> from inspect import isclass
>>> assert isclass(User), \
'Object `User` has an invalid type; expected: `class`.'
>>> assert 'authenticated' in result
>>> assert 'password' in result
>>> assert 'username' in result
>>> assert '__init__' in result
>>> assert 'login' in result
>>> assert 'logout' in result
>>> result # doctest: +NORMALIZE_WHITESPACE
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__',
'__eq__', '__firstlineno__', '__format__', '__ge__', '__getattribute__',
'__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__',
'__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
'__static_attributes__', '__str__', '__subclasshook__', '__weakref__',
'authenticated', 'login', 'logout', 'password', 'username']
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result: dict[str, str|bool]
# %% Data
class User:
def __init__(self, username, password):
self.username = username
self.password = password
self.authenticated = False
def login(self):
self.authenticated = True
def logout(self):
self.authenticated = False
alice = User('alice', 'secret')
# %% Result
result = ...
# %% About
# - Name: Idiom Dir Class
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 1. Get list of public attributes of `alice` instance (fields and methods)
# 2. Define `result: list` with the result
# 3. Run doctests - all must succeed
# %% Polish
# 1. Pobierz listę publicznych atrybutów instancji `alice` (pól i metod)
# 2. Zdefiniuj `result: list` z wynikiem
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> result
# ['authenticated', 'password', 'username']
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> from pprint import pprint
>>> from inspect import isclass
>>> assert isclass(User), \
'Object `User` has an invalid type; expected: `class`.'
>>> assert 'authenticated' in result
>>> assert 'password' in result
>>> assert 'username' in result
>>> assert '__init__' not in result
>>> assert 'login' in result
>>> assert 'logout' in result
>>> sorted(result)
['authenticated', 'login', 'logout', 'password', 'username']
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result: dict[str, str|bool]
# %% Data
class User:
def __init__(self, username, password):
self.username = username
self.password = password
self.authenticated = False
def login(self):
self.authenticated = True
def logout(self):
self.authenticated = False
alice = User('alice', 'secret')
# %% Result
result = ...
# %% About
# - Name: Idiom Dir Class
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 1. Get list of public fields of `alice` instance
# 2. Define `result: list` with the result
# 3. Run doctests - all must succeed
# %% Polish
# 1. Pobierz listę publicznych pól instancji `alice`
# 2. Zdefiniuj `result: list` z wynikiem
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> result
# ['authenticated', 'password', 'username']
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> from pprint import pprint
>>> from inspect import isclass
>>> assert isclass(User), \
'Object `User` has an invalid type; expected: `class`.'
>>> assert 'authenticated' in result
>>> assert 'password' in result
>>> assert 'username' in result
>>> assert '__init__' not in result
>>> assert 'login' not in result
>>> assert 'logout' not in result
>>> result
['authenticated', 'password', 'username']
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result: dict[str, str|bool]
# %% Data
class User:
def __init__(self, username, password):
self.username = username
self.password = password
self.authenticated = False
def login(self):
self.authenticated = True
def logout(self):
self.authenticated = False
alice = User('alice', 'secret')
# %% Result
result = ...
# %% About
# - Name: Idiom Dir Class
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 1. Get list of public methods of `alice` instance
# 2. Define `result: list` with the result
# 3. Run doctests - all must succeed
# %% Polish
# 1. Pobierz listę publicznych metod instancji `alice`
# 2. Zdefiniuj `result: list` z wynikiem
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> result
# ['login', 'logout']
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> from pprint import pprint
>>> from inspect import isclass
>>> assert isclass(User), \
'Object `User` has an invalid type; expected: `class`.'
>>> assert 'authenticated' not in result
>>> assert 'password' not in result
>>> assert 'username' not in result
>>> assert '__init__' not in result
>>> assert 'login' in result
>>> assert 'logout' in result
>>> result
['login', 'logout']
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result: dict[str, str|bool]
# %% Data
class User:
def __init__(self, username, password):
self.username = username
self.password = password
self.authenticated = False
def login(self):
self.authenticated = True
def logout(self):
self.authenticated = False
alice = User('alice', 'secret')
# %% Result
result = ...