0

A very simple problem, yet I need some assistance. I have created a class that has some default values. After that an instance takes the values based on an input. If one of the properties is "null" how can I assign the default value to it?

class Dragon:
    dragons=[]

    def __init__(self,dragon_type,name,health=2000,attack=450,fly_speed=120):
        self.dragon_type=dragon_type
        self.name=name
        self.health=health
        self.attack=attack
        self.fly_speed=fly_speed

        Dragon.dragons.append(self)

num=int(input())

for n in range(num):
    entry=input().split() #fire Azzaaxx null 480 null
    dragon_type,name,health,attack,fly_speed=entry[0],entry[1],entry[2],entry[3],entry[4]
    if health=="null":
        health=... #2000
    else:
        health=int(health)
    if attack=="null":
        attack=... #450
    else:
        attack=int(attack)
    if fly_speed=="null":
        fly_speed=... #120
    else:
        fly_speed=int(fly_speed)
    obj=Dragon(dragon_type,name,health,attack,fly_speed)

Is this approach acceptable or this should be defined differently? Thank you in advance!

2 Answers 2

3

Change the class definition so that None values are converted to the default.

class Dragon:
    dragons=[]

    def __init__(self,dragon_type,name,health=None,attack=None,fly_speed=None):
        self.dragon_type=dragon_type
        self.name=name
        self.health=health if health is not None else 2000
        self.attack=attack if attack is not None else 450
        self.fly_speed=fly_speed if fly_speed is not None else 120

        Dragon.dragons.append(self)

Then you can set the variables in the caller to None if the user enters null, and the __init__() method will do the right thing, so you don't have to repeat the default values.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! It works. Tbh I thought to organize it around None but I did not make it up to write the attributes this way.
0

Build up a dictionary of non-null values to use as keyword arguments.

args = {}
if health != "null":
    args['health'] = int(health)
if attack != "null":
    args['attack'] = int(attack)
if fly_speed != "null":
    args['fly_speed'] = int(fly_speed)


obj = Dragon(dragon_type, name, **args)

However, this is a good opportunity to move the handling of "missing" values to a class method. __init__ should require values; the class method will provide defaults when its own input is lacking.

class Dragon:
    dragons = []

    def __init__(self, dragon_type, name, health: int, attack: int, fly_speed: int):
        self.dragon_type = dragon_type
        self.name = name
        self.health = health
        self.attack = attack
        self.fly_speed = fly_speed

        Dragon.dragons.append(self)

    @classmethod
    def from_input(cls):
        entry = input().split()

        health = int(entry[2]) if entry[2] != "null" else 2000
        attack = int(entry[3]) if entry[3] != "null" else 450
        speed = int(entry[4]) if entry[4] != "null" else 120

        return cls(entry[0], entry[1], health, attack, speed)


num = int(input())

for n in range(num):
    obj = Dragon.from_input()

1 Comment

To be honest, I ignored Barmar's much-simpler answer because I had just answered a similar question involving dataclasses, where using None as a default had consequences for type checking. That's not really an issue here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.