Python – 如果变量具有特定值,则继承默认类值

huangapple go评论61阅读模式
英文:

Python - inherit default class value if the variable has a specific value

问题

一个非常简单的问题,但我需要一些帮助。我已经创建了一个类,其中有一些默认值。之后,一个实例根据输入获取这些值。如果其中一个属性为 "null",我如何将默认值分配给它?

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)

这种方法可行吗,还是应该以不同方式定义?
在此提前感谢!

英文:

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!

答案1

得分: 3

更改类定义,以便将 None 值转换为默认值。

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)

然后,如果用户输入 null,你可以将调用者的变量设置为 None__init__() 方法会自动处理默认值,这样你就不必重复默认值了。

英文:

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.

答案2

得分: 0

建立一个非空值的字典,以用作关键字参数。

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)

然而,这是一个很好的机会将处理“缺失”值移到一个类方法中。 __init__ 应该要求值;类方法将在其自己的输入缺失时提供默认值。

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()
英文:

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()

huangapple
  • 本文由 发表于 2023年2月7日 02:11:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定