Hi,

I'm new to Python, and although I've quite some programming experience, I wasn't able to find an answer for this problem:

As a first project, I am trying to create a small text game. All is going well (since it's very basic), except for my inventory system.

At the start of the game/script, I declare a global variable "inventory", and make it a list:

def start():
    global inventory
    inventory = []

Next, I created a function to display the inventory:

def inventory():
    global inventory
    print
    print "You are carrying:"
    for item in inventory:
        print " - "+item
    print

But, an error occurs: "TypeError: 'list' object is not callable". When I declare the list inside the inventory() function everything works well, so I assume this has something to do with me making the variable not global somehow..?

Any thoughts on this?

Thanks,
Lapixx

inventory is function name, you are trying to overload it by global variable name in that very function!

How about making the inventory set, makes more sense to me. global inventory is unnecessary, as you are not changing things in inventory in your function.

Also you can use:

print "You are carrying:", " - ".join(inventory)

>_> That was very silly of me. Thanks though!

Dont use global statement,it`s a ugly.
A function should keep code local and not send variabels out in global space.

Pass in argument and return in out when you need it.
Do this become to difficult move over to use a class.

def start(my_list=[]):  
    inventory = my_list
    return inventory

def inventory(arg): 
    print "You are carrying:"
    for item in arg:
        print '%s' % item 
    
def main():
    inv_item = start(['gold','food','hammer'])
    inventory(inv_item)    

if __name__ == "__main__":
    main()

I think that doing so would make things very complex when using multiple global variables that are being used in multiple functions.

Though I agree that classes would do the trick too.

Still, just wondering, what is ugly about using globals? And in what situation is using the global statement not considered ugly?

The problem with having globals is that it means that you are usually declaring something inside a function when it is going to be used outside a function so it is probably better to just declare the variable outside any sort of function at all.

It also makes it easier for someone to read your code later. One last thing that can be an issue is the fact that sometimes your global statement might be missed then causing issues in the rest of the program.

so yeah, try stick clear

:) Hope that helps

Looks unpythonic for me. Like this its OK, for my opinion.

def inventory(arg): 
    print "You are carrying:"
    for item in arg:
        print '%s' % item 
    
if __name__ == "__main__":
    inv_item = ['gold','food','hammer']
    inventory(inv_item)

More clear though to have default in beginning of file as are the imports.
This makes it for example easy to add translation for texts and to update the list in all programs parts simultaneously, when constants are not hiding inside functions (__init__ I consider exception).

english =  ['gold','food','hammer']
inv_item = english

def inventory(arg): 
    print "You are carrying:"
    for item in arg:
        print '%s' % item 
    
if __name__ == "__main__":
    inventory(inv_item)

And in what situation is using the global statement not considered ugly?

For me it`s never ok to use,and i have never used it in my code.

Looks unpythonic for me. Like this its OK, for my opinion.

It`s was just an example with function how to avoid global that was postet by Lapix.
You can off course rewrite it to one function,but that was not the point of my post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.