Fowl
Fowl

Reputation: 11

Naming a class from a variable?

I'm working on a project, and I have a team class made up of different player classes.

I want a function to create new players and add them to the team, but instead of naming the new player class something like newPlayer(), I want the class name to be the name of the player, so I don't run into issues if I create multiple new players and have a team of the same newPlayer()'s

For example, I want a function like this:

def createPlayer(player_name, player_skill):
    class player_name:
        skill = playerskill
    player_name = player_name()

So if I run:

createPlayer('Mike', 10)

I will get a class named Mike, and I can get Mike's speed with team.Mike.speed

Is it possible to get something like this?

Upvotes: 1

Views: 60

Answers (4)

Michael H.
Michael H.

Reputation: 3483

Even though I agree with @chepner, you can indeed create classes at runtime:

def createPlayer(player_name, player_skill):
    return type(player_name, (), {'skill': player_skill})

Mike = createPlayer('Mike', 10)
player = Mike()
print(player.__class__, player.skill)  # <class '__main__.Mike'> 10

Note: Mike = createPlayer('Mike', 10) is equivalent to:

class Mike:
    skill = 10

i.e. skill is a class attribute, not an instance attribute as can be seen with the following snippet:

def createPlayer(player_name, player_skill):
    return type(player_name, (), {'skill': player_skill})

Mike = createPlayer('Mike', 10)
print(Mike.skill)  # 10

player1 = Mike()
player2 = Mike()

print(player1.skill, player2.skill, Mike.skill)  # 10 10 10
player1.skill = 20
print(player1.skill, player2.skill, Mike.skill)  # 20 10 10

A quick Google search for "metaclass" might give you some more extensive sources (e.g. this or this).

Upvotes: 1

JBirdVegas
JBirdVegas

Reputation: 11413

NamedTuples might be a cleaner example

>>> from collections import namedtuple
>>> Player = namedtuple('MyPlayer', 'name skill')
>>> Player('Joe', 20)
MyPlayer(name='Joe', skill=20)
>>> jim = Player('Jim', 99)
>>> jim.skill
99

If you are just dead set on using dynamic classes then they are pretty simple to generate at runtime.

>>> Joe = type('Joe', (object,), {'skill': 20})
>>> Joe.skill
20

Upvotes: 1

Syrius
Syrius

Reputation: 941

You could define a dictionary with the name as key.

team = {}
team["Mike"] = create_player("Mike",10)

Now if you run

team["Mike"]

you will get Mike

Upvotes: 1

chepner
chepner

Reputation: 532153

You shouldn't define the class in the function; you define the function as one of the class's methods (namely, __init__):

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

player = Player("Mike", 10)

You define the class once, and create multiple instances of class.

Upvotes: 1

Related Questions