Home > OS >  How to reset a class attribute value to 0?
How to reset a class attribute value to 0?

Time:08-05

I am coding a blackjack game using OOP in Python.

One of my class attributes for both the dealer and player class is:

class Dealer:
    def __init__(self, name, balance):
        self.name = name
        self.balance = balance
        self.hand = []
        self.value = 0

class Player:
    def __init__(self, name, balance):
        self.name = name
        self.balance = balance
        self.hand = []
        self.value = 0

After each round, I want to reset my self.value back to 0 for both dealer and player.

I tried using a setter method:

def setValue(self):
    return self.value == 0

But it is obviously giving me a boolean value instead of setting the value back to 0.

Can anybody provide feedback on how best I should go about this?

CodePudding user response:

== means that you compare two values and check if they're the same. What you want to do is use = when assigning values. This is the answer:

def setValue(self):
    self.value = 0

CodePudding user response:

self.value == 0 is a condition. Its value will either True or False. and when you use return, you return that value where function is originally called.

Essentially, whenever you call setValue, you are checking if the value is 0 or not.

this is what you need to do

def setValue(self):
    self.value = 0

setter method doesn't need to return anything, it just sets the value of the attibute

CodePudding user response:

One approach is to add a reset method to each of the classes:

class Dealer:
    def __init__(self, name, balance):
        self.name = name
        self.balance = balance
        self.hand = []
        self.value = 0

    def reset(self):
        self.value = 0

class Player:
    def __init__(self, name, balance):
        self.name = name
        self.balance = balance
        self.hand = []
        self.value = 0

    def reset(self):
        self.value = 0

So if you have a d object representing dealer and p1, p2 and p3 representing players then at the start of the round you can call.

d.reset()
p1.reset()
p2.reset()
p3.reset()

This will work but it's not very clean.

Similarly, you can define reset() method in the base class since Player and Dealer are very similar. Just would like to point out that it doesn't make a whole lot of sense for Dealer to have a balance.

Alternative way is to create an external function and use duck typing so it can accept any object that defines a reset() method:

from typing import Protocol

class Resettable(Protocol):    
    def reset(self):
        pass  

def reset_multiple(objects: list[Resettable]):
    for i in objects:
        i.reset()

d = Dealer('John', 100)
p1 = Player('Jane', 120)
p2 = Player('Jim', 150)

reset_multiple([d, p1, p2])

print(d.value, p1.value, p2.value)   # Out: 0, 0, 0

Here the reset_multiple() accepts a list of any object that follows a Protocol of having a reset() method and then simply calls it.

  • Related