0

The get_objects fuction is what's being returned and showing a location, >[<main.things object at 0x000002624BB2BDF0>] This is my first time doing OOP. How do I show the actual list.

class room():

    def __init__(self, name):
        self.__exits = {}
        self.__name = name
        self.__description = None
        self.__objects = []

    def add_objects(self, things):
        self.__objects.append(things)

    def get_objects(self):
        return self.__objects

class things(room):

    def __init__(self, name, is_weapon):
        self.name = name
        self.weapon = is_weapon

    def weapon(self):
        self.is_weapon = True

    def not_weapon(self):
        self.is_weapon = False
currentRoom = center
alive = True
while alive:

    print(currentRoom.get_name())
    print(currentRoom.get_desc())
    print("Objects here: ",currentRoom.get_objects())  
Aleric G
  • 1
  • 1

1 Answers1

0

Since you say you are new to OOP, my first question would be why are you name mangling all the attributes? See this question for a discussion on python name mangling and typically why you should avoid it.

If you choose not to use name mangling, then there is no need for a 'getter' method, as you can access your objects attributes simply:

class room():

    def __init__(self, name):
        self.exits = {}
        self.name = name
        self.description = None
        self.objects = []

while alive:

    print(currentRoom.name)
    print(currentRoom.description)
    print("Objects here: ",currentRoom.objects) 

Similarly with the setter method, instead you can just use:

curretRoom.objects.append('chair')

See this question for ways to use getters and setters, but why you don't need to.

Similarly, for your things class I would suggest doing the following:

class things(room):

    def __init__(self, name, is_weapon):
        self.name = name
        self.is_weapon = is_weapon

And then querying a thing to see if it is a weapon by doing:

chair = things('chair', is_weapon=True)
print(chair.is_weapon)  # prints 'True'

And if you later decide a chair is not a weapon:

chair.is_weapon = False
print(chair.is_weapon)  # prints 'False' 
stacker
  • 108
  • 5
  • I don't really know what name mangling is, my teacher just put it in. So, the things that are in the room, can they also be in another list, in another class? Thank you. – Aleric G May 20 '20 at 11:55
  • Name mangling is the double underscore you've got (and I removed) at the front of your attribute names. It acts to hide the attributes from external users but is generally considered non-pythonic. The link I shared is a good resource along with a quick google. – stacker May 20 '20 at 22:51
  • Yes, the ```things``` objects can be attached to as many ```room``` objects as you like – stacker May 20 '20 at 22:52
  • How do I set the description? Like this, `room.description("This is room")` ? – Aleric G May 22 '20 at 13:09