Felix Leonte
Felix Leonte

Reputation: 27

automatically instantiating objects

players_list = [Ani, Paty, Felix, Alex]

class Player:

def __init__(self, name):
    self.name = name
    self.score = 0
    self.vote = 0
    self.player_hand = []
    self.choice = ''
    self.player_hand = []
            
def player_turn(self):
    print(self.name, "'s turn")


def p_vote(self):
    print(self.name, " voted")

I tried to iterate over the list, but it always gives me an error: NameError: name 'Ani' is not defined

for player in players_list:
    player = Player(str(player))

But doing all the process manually work:

Ani = Player("Ani"), etc

Is there any way that i can automate this process?

Upvotes: 0

Views: 146

Answers (3)

mark_s
mark_s

Reputation: 496

Sounds like you're trying to dynamically create variables - write code that writes code.

You could try to use the exec built-in function.

players = ['Ani', 'Paty', 'Felix', 'Alex']

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

    def p_vote(self):
        print(self.name + " voted.")

for player in players:
    exec( "%s = Player( '%s' )" %(player, player) )

Ani.p_vote()

Although, general internet advice has two points to make:

  • Be cautious where you use exec.
  • The Pythonic way is to write out the variables, "explicit is better than implicit."

Upvotes: 0

Bryce Wayne
Bryce Wayne

Reputation: 351

You are having problems with the players not being defined. So players_list = [Ani, Paty, Felix, Alex] will throw an error because the objects Ani, Paty, Felizx, and Alex do not exist.

 class Player:
    def __init__(self, name):
        self.name = name
        self.score = 0
        self.vote = 0
        self.player_hand = []
        self.choice = ''
        self.player_hand = []
            
    def player_turn(self):
        print(self.name, "'s turn")


    def p_vote(self):
        print(self.name, " voted")

Now, we need to iterate through the list.

players_list = ['Ani', 'Paty', 'Felix', 'Alex']
players = [Player(player) for player in players_list]

Upvotes: 0

dewDevil
dewDevil

Reputation: 391

First of all the thing you should know, the players_list that you have declared are not containing strings, they are being considered as variables which you have not defined anywhere, and therefore the NameError.

Now, if you want to correct this, and if you actually intend to store objects of Player in players_list, then you can do the following:

players_list = ["Ani", "Paty", "Felix", "Alex"]

class Player:

    def __init__(self, name):
        self.name = name
        self.score = 0
        self.vote = 0
        self.player_hand = []
        self.choice = ''
        self.player_hand = []
            
    def player_turn(self):
        print(self.name, "'s turn")
    
    
    def p_vote(self):
        print(self.name, " voted")
        
for i in range(len(players_list)):
    players_list[i]=Player(players_list[i])

This will store Player objects in the list you have declared just the thing that you expect to get.

Upvotes: 2

Related Questions