Python Forum
AttributeError: 'str' object has no attribute 'size'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AttributeError: 'str' object has no attribute 'size'
#4
(Nov-15-2020, 03:37 AM)deanhystad Wrote: This is the test:
orderPizza()==Pizza('M',{'garlic', 'onion', 'mushroom'})
orderPizza returns a string:
def orderPizza():
    - snip - 
    return f"Your pizza costs ${p.price()})"
And this returns a Pizza object:
Pizza('M',{'garlic', 'onion', 'mushroom'})
A Pizza object and a string will never be equal, so this test will always fail. This particular fail occurs when the Pizza.__eq__ metnod tries to compare the Pizza.size to str.size. str does not have an attribute named "size", so this throws an error.

You need to change the orderPizza function to return a Pizza.
I was fixated on the __eq__ line of the error thinking it was an issue with that code in the Class. I wouldn't have thought to look at what I was returning from the orderPizza function. This was a humbling but valuable experience.
Reply


Messages In This Thread
RE: AttributeError: 'str' object has no attribute 'size' - by russoj5 - Nov-15-2020, 05:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Object has no attribute 'replaceall' ? peterp 2 7,275 Nov-10-2020, 09:23 PM
Last Post: buran
  Calling an class attribute via a separate attribute in input wiggles 7 2,989 Apr-04-2020, 10:54 PM
Last Post: wiggles
  AttributeError: 'list' object has no attribute 'g_s' NitinL 6 3,483 Mar-31-2020, 10:24 AM
Last Post: pyzyx3qwerty
  ERROR NoneType object has no attribute content denizkb 1 2,665 Nov-21-2019, 01:18 PM
Last Post: denizkb
  AttributeError: 'tuple' object has no attribute 'move' senfik99 2 4,089 Feb-26-2019, 12:42 PM
Last Post: stullis
  text = str(text.encode('utf-8')) AttributeError: 'float' object has no attribute 'enc ulrich48155 2 8,829 Jul-31-2017, 05:21 PM
Last Post: ulrich48155

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020