Programming improvements

A few things of the many things I learned last week in Python.

My draft when a new optional dictionary was added to the method:

def method(self, arg1, options={}):

However, a better version was suggested to me:

def method(self, arg1, options=None):
options = (options or {})

And the other big learning through code review was, when constructing a string from a dictionary. My complicated code:

for key1, value1 in options.items():
    if key1 == "searchString":
        for key, value in value1.items():
            result_string = f"someString.{key}.{value}"

The suggested version is so much neater:

for key, value in options.get("searchString", {}).items():
    result_string += f"someString.{key}.{value}"

While I still feel, it would have been nice, if I’d have learned these little things before (which comes from the thought I would be liked more, and from the expectation of perfectionism by me and because of that I also think others expect perfectionism from me – which is just not achievable), it is nice to pick new things up now. While I thought in my 20s that in many areas of life I can stop learning at some point, in the last years I learned that learning will never end in any part of life, and that is good! Because if I stop being willing to learn, I will go backwards.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s