1
votes

I am doing a project centered around OOP and started wondering: Is there a difference between this?

class Trainer:
    def __init__(self, name):
        self.name = name
        self.potions = 10
        self.bag = list()
        self.team = list()
        self.current_poke = self.team[0]

as opposed to doing this:

class Trainer:
    def __init__(self, name, potions, bag, team, current_poke):
        self.name = name
        self.potions = potions
        self.bag = bag
        self.team = team
        self.current_poke = current_poke

Is one version better than the other, and if so why. I know that one allows you to define more specific values for each variable each time you instantiate an object. But are there any operational/efficiency differences between the two?

No. Really, how could there be? It's just assigning object references. Usually, the second option is a bit better because it gives you more flexibility in the future, but there's no performance difference. In addition, initialization is rarely an important performance bottleneck, unless you're creating millions of objects. - Tim Roberts
self.team[0] will get an error, since self.team is empty. - Barmar
@TimRoberts Thank you for clearing that up! I just wanted to make sure before I kept going. - cginnaty