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.