This is the code I' ve been working on:
define the Vehicle 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())
I want to make this shorter using init but I haven't succeeded ye
CodePudding user response:
If the __init__
method is simple like this, you could a dataclass.
Directly answering your question, would be this:
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)
But there are several improvements you could make to this.
- Limited kinds of vehicle
Currently,
kind
can be any string, but presumably there are only a limited number of kinds of vehicle, so you could restrict to these. I would use anEnum
. - You have to specify everything even if there might be defaults
- Description could be a property
- Using formatted strings (the new standard)
Putting that all together would look like this:
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
CodePudding user response:
Create an __init__
function, which takes self
as an argument the arguments passed to the instance that you cerate of 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())
You can read more about this in the official Python documentation here.
PS: you can remove the variables that you put at the top of the Vehicle
class as they can be defined in __init__
.
CodePudding user response:
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)