__dict__ Printing Format Issue Encountered

Hello,

I encountered a printing issue as I was working out an exercise when printing an instance.
In the following example, please note the following line:

print(f'{key:<10} {self.__dict__[key]:>10}')  # Particularly: ':>10'

For some reason adding the :>n appears to cause an exception. Is this a bug?

Here is the sample program:

class Person:

    def __init__(self, name, age, pay = 0, job = None):

        self.name = name
        self.age = age
        self.pay = pay
        self.job = job

    def lastName(self):
        return self.name.split()[-1]

    def giveRaise(self, percent):
        self.pay *= (1.0 + percent)

    def __str__(self):
        for key in self.__dict__:
            # Only uncomment one print statement one at a time for testing
            # print(f'{key:<10} {self.__dict__[key]:>10}')
            # print(f'{key:<10} {self.__dict__[key]}')
            print(key, self.__dict__[key])
        return '<%s => %s>' % (self.__class__.__name__, self.name)

class Manager(Person):

    def giveRaise(self, percent, bonus = 0.1):
        # self.pay *= (1.0 + percent + bonus)
        Person.giveRaise(self, percent + bonus)

if __name__ == '__main__':

    # Create a record
    tom = Manager(name = 'Tom Doe', age = 50, pay = 50000)
    print(tom)

If I perform the same operation in a regular dictionary, this is not an issue:

h = {'one':56, 'two':345}
for key in h:
    print(f'{key:<7} {h[key]:>10}')

Can someone please advise.

Thank you.

Try reversing the order of your test lines:

print(f'{key:<10} {self.__dict__[key]}') # this one first because it works
print(f'{key:<10} {self.__dict__[key]:>10}')

It’s because job is None and the formatting string {:>10} is not valid for None.

Hello,

thank you for responding to my query. I did swap them as per your recommendation. An exception is still generated. Please note, however, that the objective is to have only one and not multiple print statements. There were extra print statements for testing and comparison purposes only. Only one print statement should be uncommented.

    print(f'{key:<10} {self.__dict__[key]:>10}')
                      ^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: unsupported format string passed to NoneType.__format

Why does the same format work for a regular dictionary as per the example that I provided at the end of my post but not when using self.__dict__[key]?

I apologize but I do not understand what you mean by because job is None. Can you please elaborate.

Thank you for any additional clarification that you may provide.

It is trying to print out the key job, which is part of your example (self.job). You didn’t set that value, so it has the default value from the your __init__, which is None. The format string is not valid for None, it only works on a str, so you get a error.

My suggestion to reverse the order was so that you could inspect the output and see what immediately preceded the exception: it would be the line job None, showing you that this is the value causing the problem.

1 Like

Ok, got it. Just had the eureka moment.

Thank you for your time, patience and sticking with this.

Much obliged. :raised_hands: