[Python] How do I define two mutually dependent parameters in Python?

Discussão em 'Python' iniciado por Stack, Maio 14, 2025.

  1. Stack

    Stack Membro Participativo

    I want to create a Python class with two mutually inclusive parameters that are dependent on each other. The user must either provide a value for both parameters or neither. If one parameter is specified without the other, an exception should be raised.

    class TestEvent:
    def __init__(self, start_date = None, end_date = None , **kwargs):
    # Check if start_date and end_date are defined
    pass

    valid_event_1 = TestEvent()
    valid_event_2 = TestEvent(start_date = '2022-07-01', end_date = '2022-08-01')
    invalid_event_1 = TestEvent(start_date = '2022-07-01') # Raise exception
    invalid_event_2 = TestEvent(end_date = '2022-08-01') # Raise exception


    How can I define two mutually dependent parameters in Python?

    Continue reading...

Compartilhe esta Página