V
V
Vladislav Polyakov2018-02-21 23:13:28
Python
Vladislav Polyakov, 2018-02-21 23:13:28

How to resolve such error in python?

class ClientInfo:
    def __init__(
            self,
            last_name: str = None,
            first_name: str = None,
            middle_name: str = None,
            gender: int = None,
            birth_date: OffsetDateTime = None,
            **kwargs):
        print(kwargs)

        self.last_name = kwargs['lastName'] if kwargs else last_name
        self.first_name = kwargs['firstName'] if kwargs else first_name
        self.middle_name = kwargs['middleName'] if kwargs else middle_name
        self.gender = kwargs['gender'] if kwargs else gender
        self.birth_date = kwargs['birthDate'] if kwargs else birth_date


print(ClientInfo(**{'gender': '0', 'lastName': 'L', 'middleName': 'M', 'firstName': 'F',
                    'birthDate': OffsetDateTime('YYYY-MM-DD')
                    }).last_name)

I get an error when I try to run the file
Traceback (most recent call last):
{'lastName': 'L, 'middleName': 'M', 'firstName': 'МF, 'birthDate': <app.time.OffsetDateTime object at 0x7fbe9a712e80>}
  File "/home/cgrass/PycharmProjects/hivetaxi/app/reg.py", line 30, in <module>
    'birthDate': OffsetDateTime('YYYY-MM-DD')
  File "/home/cgrass/PycharmProjects/hivetaxi/app/reg.py", line 25, in __init__
    self.gender = kwargs['gender'] if kwargs else gender
KeyError: 'gender'

Process finished with exit code 1

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-02-22
@polRk

Apparently there is no gender key in kwargs. Either check for it or get it securely
self.gender = kwargs.get('gender', gender)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question