1

I was making a simple method that would return a list of a classes attribute values. It works fine but at the end of the list being printed there is always 'None' displayed as well. I am not sure why. Any clarification?

Here is my code: Player Class

class Player:
    def __init__(self, name, caps, inventory, caravans):
        self.name = name
        self.caps = caps
        self.inventory = inventory
        self.caravans = caravans

Inventory Class and it's methods, I am referring to the list method.

class Inventory:
    """Items are stored by value not quantity"""
    def __init__(self, weapons_ammo, meds_chems, armor):
        self.weapons_ammo = weapons_ammo
        self.meds_chems = meds_chems
        self.armor = armor

    def list(self):
        """List all inventory values."""
        print(f'Weapons & Ammo: {self.weapons_ammo}\n'
              f'Meds & Chems: {self.meds_chems}\n'
              f'Armor: {self.armor}')

Instances

example_inventory = Inventory(5000, 2000, 1500)

example_player = Player('John', 10000, example_inventory, 3)

When I run this code:

print(example_player.inventory.list())

I get:

Weapons & Ammo: 5000 Meds & Chems: 2000 Armor: 1500 None

But when I run it without the print function:

example_player.inventory.list()

I get the proper return I was looking for with the 'None' at the end.

Could anyone explain why this is? I'm assuming it has to do with the method already calling the print function.

Thank you!

1
  • .list() only returns None, it doesn't return anything else. It prints something, though. Commented Feb 3, 2021 at 3:04

2 Answers 2

2

You specifically printed the return value of the function:

print(example_player.inventory.list())

Your list function has no explicit return; the default value is None. This has nothing to do with having another print insides the function, merely that you didn't return anything.

I think that all you want to do is to execute the function:

example_player.inventory.list()

BTW, it's very bad form to "shadow" a built-in type: don't use list as a function name. Something such as show_inventory would be better.

Sign up to request clarification or add additional context in comments.

3 Comments

If OP isn't planning on needing any other information display functions for the Inventory class, they might consider implementing __str__ so they can simply print(example_player.inventory).
Conversely, if OP will need to deal with the contents of the inventory, they might consider implementing __iter__ so they can go for item in eample_player.inventory and possibly __getitem__ and potentially __setitem__ etc
Both are good additions to my beginning suggestions.
1

print(example_player.inventory.list()) prints the return value of the example_player.inventory.list() method; since there's no return statement in the method, the return value is None.

The print inside the method prints "Weapons & Ammo: 5000 Meds & Chems: 2000 Armor: 1500" and then the print outside prints "None"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.