Python Forum
Help with lists and class objects
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with lists and class objects
#11
On line 30, the indentation is incorrect. __str__() should not be nested under __init__(). Also, you still need the __repr__() method. These two often seem interchangeable, but they are called by different functions; for instance, print() calls obj.__repr__() whereas str() calls obj.__str__().
Reply
#12
(Jan-14-2020, 11:25 PM)stullis Wrote: print() calls obj.__repr__() whereas str() calls obj.__str__().
A correction here,print() call __str__() the same do str().
So in my code before in Thread.
>>> # Call __str__
>>> print(sword)
The item is: sword
>>> str(sword)
'The item is: sword'

>>> # Call __repr__
>>> sword
Items('sword')
>>> repr(sword)
"Items('sword')"
__str__ method should primarily be human readable.

__repr__ goal is to be unambiguous,and should be as explicit as possible about what this object is.
The resulting string is intended more as help for developers and debugging.
Reply
#13
In addition to too much indentation on your __str__() definition you are using format strings wrong. What you have now will always return the string literal "self.item". On line 31 you are returning self.item which is already a string so just return it, remove the f and the two double quotes:
def __str__(self):
     return self.item
Also on line 351 you are not calling your Starter.list_items method, you are printing it. Should be:
def inventory():
    Starter.list_items()
While I applaud your attempt to make this game Object Oriented you must have realized that simply creating several classes with the exact same code probably isn't right. The whole point of OOP would be to make a base class called "Crate" with all the common functionality and then inheriting from it in more specific classes which should only contain the things that are different from the base class. Inheritance is the first "Pillar of OOP" and it makes coding games like this a lot easier so you need to understand how to use it.
"So, brave knights, if you do doubt your courage or your strength, come no further, for death awaits you all with nasty, big, pointy teeth!" - Tim the Enchanter
Reply
#14
Thank you guys! you've helped me a lot! sorry if I was annoying, I am still attempting to learn Python because I'd like to do it as a job eventually :)
Reply
#15
No need to apologize. Learning and helping others learn is what a lot of us are here for. That and the spam, spam, spam. Lovely Spam! Heart

Take a look at class inheritance next and code a proper base class for your Crates which can become the parent class for all the crate types.

I suggest you work in a sandbox environment with just the classes and some test code for now rather than trying to incorporate everything directly into your game. OOP is one of those things you really to need grok before you try to use it.
"So, brave knights, if you do doubt your courage or your strength, come no further, for death awaits you all with nasty, big, pointy teeth!" - Tim the Enchanter
Reply
#16
Thanks a lot! I'll definitely look into that :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I access objects or widgets from one class in another class? Konstantin23 3 995 Aug-05-2023, 08:13 PM
Last Post: Konstantin23
  Creating list of lists, with objects from lists sgrinderud 7 1,611 Oct-01-2022, 07:15 PM
Last Post: Skaperen
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,360 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Class objects Python_User 12 4,419 Aug-27-2020, 08:02 PM
Last Post: Python_User
  How to create and define in one line a 2D list of class objects in Python T2ioTD 1 2,029 Aug-14-2020, 12:37 PM
Last Post: Yoriz
  printing class properties from numpy.ndarrays of objects pjfarley3 2 1,941 Jun-08-2020, 05:30 AM
Last Post: pjfarley3
  How to serialize custom class objects in JSON? Exsul1 4 3,480 Sep-23-2019, 08:27 AM
Last Post: wavic
  How do I write class objects to a file in binary mode? Exsul1 7 5,733 Sep-14-2019, 09:33 PM
Last Post: snippsat
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,252 Mar-20-2019, 08:01 PM
Last Post: stillsen
  Do objects get their own copy of the class methods? Charles1 1 2,085 Feb-02-2019, 04:40 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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