Jan-27-2022, 01:12 PM
(Jan-26-2022, 07:29 PM)deanhystad Wrote: __init__ not __int__. Hard to see because you often see what you expect to see, not what is really there.
If an attribute created in __init__() is not in the object, test if __init__() is called.
class Portfolio: def __int__(self): print("Enter __init__()") self.holdings = {} #key = ticker, Value = number of shares print("Leave __init__()")When I created a Portfolio object it didn't print anything. That means __init__() wasn't called. Knowing that it didn't take long to find the error.
I saw with the Python tutor https://pythontutor.com/visualize.html#mode=display something was wrong with that part of the code: def __int__(self):
It also return AttributeError: 'Portfolio' object has no attribute 'holdings'
How to solve it is something else.
I am not sure what is going on here:
str_len = len(max(p)[0]) for (ticker, shares) in p: if len(ticker) < str_len: spacer = ' '*(str_len - len(ticker) + 2) else: spacer = ' '*2 print(f'{ticker} {spacer} {shares}')Menator spoke about a hidden character.