Posts: 201
Threads: 37
Joined: Dec 2021
Hello,
I am trying to understand what is going on:
class Portfolio:
def __int__(self):
self.holdings = {} #key = ticker, Value = number of shares
def buy(self, ticker, shares):
self.holdings[ticker] = self.holdings.get(ticker, 0) + shares
def sell(self, ticker, shares):
self.holdings[ticker] = self.holdings.get(ticker, 0) - shares
def __iter__(self):
return iter(self.holdings.items())
p = Portfolio()
p.buy('Alpha', 15)
p.buy('Beta', 23)
p.buy('Gamma', 9)
p.buy('Gamma', 20)
p.sell('Beta', 5)
for (ticker, shares) in p:
print(ticker, shares) According to the errors output:
Error: Traceback (most recent call last):
File "<string>", line 19, in <module>
File "<string>", line 9, in buy
AttributeError: 'Portfolio' object has no attribute 'holdings'
>
I am missing a module? But which one?
Thank you
Posts: 1,144
Threads: 114
Joined: Sep 2019
Jan-26-2022, 04:41 PM
(This post was last modified: Jan-26-2022, 04:52 PM by menator01.)
My guess is you have a hidden character.
I tried your code from copy and paste and got the same error but, when I typed it in it worked as expected.
class Portfolio:
def __init__(self):
self.holdings = {}
def buy(self, ticker, shares):
self.holdings[ticker] = self.holdings.get(ticker, 0) + shares
def sell(self, ticker, shares):
self.holdings[ticker] = self.holdings.get(ticker, 0) - shares
def __iter__(self):
return iter(self.holdings.items())
p = Portfolio()
p.buy('Alpha', 15)
p.buy('Beta', 23)
p.buy('Gamma', 9)
p.buy('Gamma', 20)
p.sell('Beta', 5)
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}') Output: Alpha 15
Beta 18
Gamma 29
Posts: 201
Threads: 37
Joined: Dec 2021
(Jan-26-2022, 04:41 PM)menator01 Wrote: My guess is you have a hidden character.
I tried your code from copy and paste and got the same error but, when I typed it in it worked as expected.
class Portfolio:
def __init__(self):
self.holdings = {}
def buy(self, ticker, shares):
self.holdings[ticker] = self.holdings.get(ticker, 0) + shares
def sell(self, ticker, shares):
self.holdings[ticker] = self.holdings.get(ticker, 0) - shares
def __iter__(self):
return iter(self.holdings.items())
p = Portfolio()
p.buy('Alpha', 15)
p.buy('Beta', 23)
p.buy('Gamma', 9)
p.buy('Gamma', 20)
p.sell('Beta', 5)
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}') Output: Alpha 15
Beta 18
Gamma 29
Thanks menator for solving that. Which i knew where it came from, from the start.
Posts: 6,783
Threads: 20
Joined: Feb 2020
__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.
Frankduc and BashBedlam like this post
Posts: 201
Threads: 37
Joined: Dec 2021
(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.
Posts: 6,783
Threads: 20
Joined: Feb 2020
Jan-27-2022, 02:21 PM
(This post was last modified: Jan-27-2022, 04:47 PM by deanhystad.)
Menator was wrong. It was a missing character, not a hidden character. He didn't see the method name was __int__ either.
What part of the code are you confused about? Be more specific.
Stop using "reply" all the time. Haven't you noticed that nobody uses it when replying to your posts? Use "Quick Reply" and if it is unclear what post you are replying to, reference the poster's name or copy/paste/quote the part you are referencing. Replying to everything ends up making really long threads that are annoying to read.
Posts: 201
Threads: 37
Joined: Dec 2021
But what is missing in the code to make that part work?
for (ticker, shares) in p:
print(ticker, shares) Anyway ill find out soon or later
thanks
Posts: 6,783
Threads: 20
Joined: Feb 2020
Jan-27-2022, 04:47 PM
(This post was last modified: Jan-27-2022, 05:16 PM by deanhystad.)
The thing that was missing was the second "i" in "__in it__()". Your Portfolio class did not have an __init__() method because you made a mistake when typing the method name. Instead you wrote a __int__() method which was never used.
When you created a Portfolio object Python looked to see if the Portfolio class has an __init__() method. It did not because of your typing error. This resulted in the holdings dictionary never being created.
What is your question about this code?
for (ticker, shares) in p:
print(ticker, shares) If you fix the typing error in the Portfolio.__init__() method name the loop will work. If you don't fix the method name your program never reaches this point in the code because it crashes when you call Portfolio.buy().
Posts: 201
Threads: 37
Joined: Dec 2021
To say i tripled check and more to see if there was mistyping.
Its not this code it is the one provided by menator:
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}') str is for string and len is for length (maximum(p)) Whatever i thought it was his solution to solve the mystery behind the absence of output and error returned.
When actually it is just a way to create a space between the porfolio stocks and prices in the output.
Sometimes i can be dense!
Thanks dean
Posts: 1
Threads: 0
Joined: Mar 2023
It's strange that you're seeing the identical network issue on two systems that are purportedly completely distinct from one another. It's possible that it was merely a coincidence. Nerdle is a fun and challenging way to sharpen your analytical and numerical prowess. Are you able to recall anything that altered itself about the same time that this phenomenon began to take place?
|