Reputation: 301
When I construct a __init__()
in a Car class, say, I want to check that variables make
and current_gas
to be string and float or integer (not negative), respectively.
Then how do I raise an appropriate error for each variable?
I tried
class Car:
def __init__(self,make,current_gas):
if type(make) != str:
raise TypeError("Make should be a string")
if not type(current_gas) == float or type(current_gas) == int:
raise TypeError("gas amount should be float or int")
if current_gas <=0:
raise ValueError("gas amount should not be negative")
However, this __init__()
is not working properly. How do I fix it?
Upvotes: 1
Views: 872
Reputation: 23634
Looks like your boolean logic on the second if statement is wrong (not
needs to be around both checks), however, you can use isinstance
to simplify checking multiple types.
You also used current_gas_level
instead of current_gas
. Try something like this:
class Car:
def __init__(self, make, current_gas):
if not isinstance(make, str):
raise TypeError("Make should be a string")
if not isinstance(current_gas, (int, float)):
raise TypeError("gas amount should be float or int")
if current_gas <= 0:
raise ValueError("gas amount should not be negative")
self.make = make
self.current_gas = current_gas
To make the definition a little easier to follow, I might also suggest using a dataclass and a __post_init__
to do the validation.
from dataclasses import dataclass
from typing import Union
@dataclass
class Car:
make: str
current_gas: Union[int, float]
def __post_init__(self):
if not isinstance(self.make, str):
raise TypeError("Make should be a string")
if not isinstance(self.current_gas, (int, float)):
raise TypeError("gas amount should be float or int")
if self.current_gas <= 0:
raise ValueError("gas amount should not be negative")
Upvotes: 4