簡體   English   中英

如何使用 __init__ 縮短此代碼?

[英]How can I make this code shorter using __init__?

這是我一直在處理的代碼:

定義車輛 class

class Vehicle:
    name = ""
    kind = "car"
    color = ""
    value = 100.00
    def description(self):
        desc_str = "%s is a %s %s worth $%.2f." % (self.name, self.color, self.kind, self.value)
        return desc_str
# your code goes here
car1 = Vehicle()
name = "Fer"
kind = "convertible"
color = "red"
value = 60,000.00
car2 = Vehicle()
name = "Jump"
kind = "van"
color = "blue"
value = 10,000.00
# test code
print(car1.description())
print(car2.description())

我想使用init縮短這個時間,但我還沒有成功

創建一個__init__ function,它將self作為參數 + arguments 傳遞給您創建的Vehicle實例:

class Vehicle:    
    def __init__(self, name, kind, color, value):
        self.name = name
        self.kind = kind
        self.color = color
        self.value = value
    
    def description(self):
        desc_str = "%s is a %s %s worth $%.2f" % (self.name, self.color, self.kind, self.value)
        return desc_str
    

# your code goes here
car1 = Vehicle(name = "Fer",
               kind = "convertible",
               color = "red",
               value = 60000)
car2 = Vehicle(name = "Jump",
               kind = "van",
               color = "blue",
               value = 10000)

# test code
print(car1.description())
print(car2.description())

您可以在 此處的官方 Python 文檔中閱讀更多相關信息。

PS:您可以刪除放在Vehicle class 頂部的變量,因為它們可以在__init__中定義。

如果__init__方法像這樣簡單,您可以使用dataclass

直接回答你的問題,是這樣的:

from dataclasses import dataclass

@dataclass
class Vehicle:
    name: str
    kind: str
    color: str
    value: float

    def description(self) -> str:
        return "%s is a %s %s worth $%.2f." % (self.name, self.color, self.kind, self.value)

v1 = Vehicle("Fer", "convertible", "red", 60_000.0)
v2 = Vehicle("Jump", "van", "blue", 10_000.0)

但是您可以對此進行一些改進。

  • 車輛種類有限 目前, kind可以是任何字符串,但大概只有車輛種類有限,因此您可以限制這些。 我會使用Enum
  • 即使可能存在默認值,您也必須指定所有內容
  • 描述可能是一個屬性
  • 使用格式化字符串(新標准)

把它們放在一起看起來像這樣:

from dataclasses import dataclass
from enum import Enum

class VehicleKind(Enum):
    car = "car"
    van = "van"
    convertible = "convertible"


@dataclass
class Vehicle:
    name: str
    color: str
    kind: VehicleKind = VehicleKind.car
    value: float = 100.0

    @property
    def description(self) -> str:
        return f"{self.name} is a {self.color} {self.kind.value} worth {self.value:.2f}"

v1 = Vehicle("Fer", "red", VehicleKind.convertible, 60_000.0)
v2 = Vehicle("Jump", "blue", VehicleKind.van, 10_000.0)

print(Vehicle("Default", "green").description)
# Default is a green car worth 100.00
class Vehicle:
    def __init__(self, name, kind, color, value):
        self.name = name
        self.kind = kind
        self.color = color
        self.value = value

    def __str__(self):  # this makes it a string if you want to print something
        return "%s is a %s %s worth $%.2f." % (self.name, self.color, self.kind, self.value)


# your code goes here
car1 = Vehicle(name="Fer", kind="convertible", color="red", value=60000.00)
car2 = Vehicle(name="Jump", kind="van", color="blue", value=10000.00)

# test code
print(car1) # the __str__ makes it so we don't have to call a function for this.
print(car2)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM