Instantiating an object in a function - Python
Question: I'm relatively new to python but think I have a decent enough understanding,
except for (apparently) the correct way to use the "import" statement. I
assume that's the problem, but I don't know.

I have

    
    
    from player import player
    
    def initializeGame():
        player1 = player()
        player1.shuffleDeck()
        player2 = player()
        player2.shuffleDeck()
    

and

    
    
    from deck import deck
    
    class player(object):
        def __init__(self):
            self.hand = []
            self.deck = deck()
    
        def drawCard(self):
            c = self.deck.cards
            cardDrawn = c.pop(0)
            self.hand.append(cardDrawn)
    
        def shuffleDeck(self):
            from random import shuffle
            shuffle(self.deck.cards)
    

But when i try to initializeGame() it says "player1 has not been defined" and
I'm not really sure why. In that same file if I just use "player1 = player()"
then it woks perfectly fine but it refuses to work inside of a function. Any
help?

EDIT: ADDING THINGS THAT WEREN'T INCLUDED BEFORE

    
    
    class deck(object):
        def __init__(self):
            self.cards = []
    
        def viewLibrary(self):
            for x in self.cards:
                print(x.name)
    
        def viewNumberOfCards(self, cardsToView):
            for x in self.cards[:cardsToView]:
                print(x.name)
    
    
    from deck import deck
    
    class player(object):
        def __init__(self):
            self.hand = []
            self.deck = deck()
    
        def drawCard(self):
            c = self.deck.cards
            cardDrawn = c.pop(0)
            self.hand.append(cardDrawn)
    
        def shuffleDeck(self):
            from random import shuffle
            shuffle(self.deck.cards)
    

and the traceback error is

    
    
    player1.deck.cards
    
    Traceback (most recent call last):
      File "<pyshell#2>", line 1, in <module>
        player1.deck.cards
    NameError: name 'player1' is not defined
    


Answer: 
    Traceback (most recent call last):
      File "<pyshell#2>", line 1, in <module>
        player1.deck.cards
    NameError: name 'player1' is not defined
    

This shows the line where the error is thrown: `player1.deck.cards`. Said line
is not in the code you gave us so we can only make assumptions on why you get
the exception.

However, it is very likely, that your script looks somehow like this:

    
    
    initializeGame()
    
    # and then do something with
    player1.deck.cards
    

This however does not work, as `player1` and `player2` are only local
variables inside the `initializeGame` function. As soon as the function
returns no more references to them are left and they are most likely pending
for garbage collection.

So if you want to access those objects, you have to make sure that they stay
around. You could do this by having the variables globally, or you could
simply return them from your `initializeGame` function:

    
    
    def initializeGame():
        player1 = player()
        player1.shuffleDeck()
        player2 = player()
        player2.shuffleDeck()
        return player1, player2
    

Then you can just call it like this:

    
    
    player1, player2 = initializeGame()
    

And have local references to the created objects.

Or even better, create an object that represents the whole game, where the
players are instance variables:

    
    
    class Game:
        def __init__ (self):
            self.player1 = player()
            self.player1.shuffleDeck()
            self.player2 = player()
            self.player2.shuffleDeck()
    

Then you can just create a `Game` instance and access the players using
`game.player1` or `game.player2`. And of course, having an object for the game
itself allows you to encapsulate a lot game related functions into the object
as well.

