Skip to content
Foxipa
Code Completion 4 Challenges

Complete the Python Module Attribute Access Code

Practice defining module fallback values, raising AttributeError, exposing dynamic names through dir(), and caching a lazily resolved value.

Advanced ~9 min read

Challenge 1

Return the module version when the requested name is version.

python Your answer
def resolve(name):
    if name == "version":
        return 
    raise AttributeError(name)
print(resolve("version"))

Press Ctrl⌘Enter to check your answer

Challenge 2

Raise the exception used to signal that an unknown module attribute is unavailable.

python Your answer
def resolve(name):
    if name == "ready":
        return True
    
try:
    resolve("missing")
except AttributeError:
    print("not available")

Press Ctrl⌘Enter to check your answer

Challenge 3

Return an iterable listing these dynamically available names.

python Your answer
def __dir__():
    return 
print(__dir__())

Press Ctrl⌘Enter to check your answer

Challenge 4

Store the resolved object in module globals so the next lookup can find it normally.

python Your answer
def resolve(name):
    if name == "answer":
        value = 42
        
        return value
    raise AttributeError(name)
print(resolve("answer"))

Press Ctrl⌘Enter to check your answer