Challenge - 5 Problems
Custom Signals Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a custom signal is sent without any receivers?
Consider a Django custom signal defined and sent, but no receiver functions are connected to it. What is the behavior when the signal is sent?
Django
from django.dispatch import Signal my_signal = Signal() my_signal.send(sender=None)
Attempts:
2 left
💡 Hint
Think about what happens if no one is listening when you shout.
✗ Incorrect
When a Django signal is sent but no receivers are connected, the send method returns an empty list indicating no receivers handled the signal. No error is raised.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a custom Django signal with providing arguments?
You want to create a custom Django signal that sends two arguments: 'user' and 'action'. Which code snippet correctly defines this signal?
Attempts:
2 left
💡 Hint
Check the exact keyword argument name for declaring arguments in Django signals.
✗ Incorrect
The correct keyword argument to specify the arguments a signal provides is 'providing_args'. Other options are invalid and cause errors.
🔧 Debug
advanced3:00remaining
Why does this custom signal receiver not get called?
Given the code below, why does the receiver function never execute when the signal is sent?
Django
from django.dispatch import Signal, receiver my_signal = Signal(providing_args=['data']) @receiver(my_signal) def my_receiver(sender, **kwargs): print('Received:', kwargs.get('data')) # Somewhere else in code my_signal.send(sender=None, data='Hello')
Attempts:
2 left
💡 Hint
Think about how Django connects signals and when decorators run.
✗ Incorrect
Django connects signal receivers when the module containing them is imported. If the module is not imported, the receiver is never registered and won't be called.
❓ state_output
advanced3:00remaining
What is the output of this custom signal with multiple receivers?
Given the code below, what will be printed when the signal is sent?
Django
from django.dispatch import Signal, receiver my_signal = Signal(providing_args=['value']) @receiver(my_signal) def receiver_one(sender, **kwargs): print('One:', kwargs.get('value')) return 'one' @receiver(my_signal) def receiver_two(sender, **kwargs): print('Two:', kwargs.get('value')) return 'two' results = my_signal.send(sender=None, value=42) print(results)
Attempts:
2 left
💡 Hint
Recall what the send method returns: a list of tuples with receiver references and their return values.
✗ Incorrect
The send method returns a list of tuples. Each tuple contains the receiver function object (with memory address) and its return value. The printed output shows function references with memory addresses.
🧠 Conceptual
expert2:30remaining
Which statement about Django custom signals is TRUE?
Select the only true statement about Django custom signals.
Attempts:
2 left
💡 Hint
Think about what the send method returns after notifying receivers.
✗ Incorrect
The send method returns a list of tuples, each containing a receiver function and its return value. Other options are false: sender can be None, receivers cannot modify sender for others, and Django does not auto-connect custom signals to model events.