Python 如何以列表的形式访问、修改和验证子类中的值
如果主题行没有准确描述我要做的事情,请原谅。我不知道还能用什么来形容它 我有一个包含子类的类。在子类中有一个值,它是一个列表。使用我的主类,我希望能够检索和设置列表中的每个值。下面的代码是我试图做的一个示例,显然不起作用: 在这个例子中,我有一辆汽车。每辆车有四个轮胎。我想能够检索和设置的轮胎类型对每个车轮的我的汽车分别。我还想验证,以防止我把一个不需要的轮胎类型(火石)我的车Python 如何以列表的形式访问、修改和验证子类中的值,python,list,class,Python,List,Class,如果主题行没有准确描述我要做的事情,请原谅。我不知道还能用什么来形容它 我有一个包含子类的类。在子类中有一个值,它是一个列表。使用我的主类,我希望能够检索和设置列表中的每个值。下面的代码是我试图做的一个示例,显然不起作用: 在这个例子中,我有一辆汽车。每辆车有四个轮胎。我想能够检索和设置的轮胎类型对每个车轮的我的汽车分别。我还想验证,以防止我把一个不需要的轮胎类型(火石)我的车 class Car: def __init__(self, model: str): se
class Car:
def __init__(self, model: str):
self._model = model
self._tires = self.Tires()
class Tires():
def __init__(self):
# Default to no tires on the car
self._tires = ["", "", "", ""]
# Create my car
my_car = Car("LeSabre")
# Put four tires on my car. I don't want Firestones, so it should error on the last two tires. How to validate???
my_car.Tires[0] = "Goodyear"
my_car.Tires[1] = "Goodyear"
my_car.Tires[2] = "Firestone"
my_car.Tires[3] = "Firestone"
# What is the second tire on my car?
print(my_car.Tires[1])
您可以使用
UserList
python库。其项存储在数据
属性的基础列表中
修改的源代码:
import collections
from collections import UserList
class Car:
def __init__(self, model: str):
self._model = model
self._tires = self.Tires()
class Tires(UserList):
def __init__(self):
self.data = ["", "", "", ""]
my_car = Car("LeSabre")
my_car._tires[0] = "Goodyear"
my_car._tires[1] = "Goodyear"
my_car._tires[2] = "Firestone"
my_car._tires[3] = "Firestone"
print(my_car._tires[1])
打印固特异谢谢您的评论。它帮助我了解我想做的事情。这就是我想到的。它可能不是最好的,也不是最漂亮的,但它很有效 我绝对愿意对这段代码发表评论
class Car:
def __init__(self, model: str):
self._model = model
self._tires = ["", "", "", ""]
def tires(self, tires: list = None) -> list:
"""
:param tires: A list of four tires to put on the car
:return: A list of tires on the car
"""
# If we specified some tires...
if tires is not None:
# ... there must be 4 of them
if len(tires) != 4:
raise Exception("Wrong number of tires for a car")
# Change all four tires
for t in range(0, 4):
self.tire(t, brand=tires[t])
return self._tires
def tire(self, index:int, brand: str = None):
"""
:param self:
:param index: Which tire are we accessing
:param brand: What brand of tire are we putting on
:return: The brand of tire at that index
"""
# There can only be four tires
if index < 0 or index > 3:
raise Exception("Invalid tire selected")
if brand is not None:
# Make sure they don't put a Firestone on my car
if brand == "Firestone":
raise Exception("Don't use Firestone tires")
else:
self._tires[index] = brand
return self._tires[index]
# Create my car
my_car = Car("LeSabre")
# Put four tires on my car. I don't want Firestones, so it should error on the last two tires. How to validate???
my_car.tire(0, "Goodyear")
my_car.tire(1, "Goodyear")
my_car.tire(2, "Uniroyal")
my_car.tire(3, "Uniroyal")
# What is the third tire on my car?
print(my_car.tire(2))
# What are all the tires on my car?
print(my_car.tires())
# Put a Firestone on my car
my_car.tire(1, "Firestone")
等级车:
定义初始化(self,型号:str):
自我。_模型=模型
self.\u轮胎=[“”,“”,“”,“”,“”]
def轮胎(自身,轮胎:列表=无)->列表:
"""
:param轮胎:要安装在汽车上的四个轮胎列表
:return:汽车轮胎列表
"""
#如果我们指定一些轮胎。。。
如果轮胎不是无:
# ... 一定有四个
如果len(轮胎)!=4:
raise异常(“汽车轮胎数量错误”)
#更换所有四个轮胎
对于范围(0,4)内的t:
自身轮胎(t,品牌=轮胎[t])
返回自我。\u轮胎
def轮胎(自身,索引:int,品牌:str=无):
"""
:参数self:
:参数索引:我们正在访问哪个轮胎
当前位置param brand:我们要穿什么牌子的轮胎
:return:该索引处的轮胎品牌
"""
#只能有四个轮胎
如果指数<0或指数>3:
升起异常(“选择的轮胎无效”)
如果品牌不是无:
#确保他们没有在我的车上放火石
如果品牌==“火石”:
引发异常(“不要使用Firestone轮胎”)
其他:
自身轮胎[指数]=品牌
返回自。_轮胎[索引]
#创造我的汽车
my_car=汽车(“LeSabre”)
#给我的车装上四个轮胎。我不想要火石,所以最后两个轮胎会出错。如何验证???
我的汽车轮胎(0,“固特异”)
我的汽车轮胎(1,“固特异”)
我的汽车轮胎(2,“Uniroyal”)
我的汽车轮胎(3,“Uniroyal”)
#我车的第三个轮胎是什么?
打印(我的车轮胎(2))
#我车上的轮胎都是什么?
打印(我的车轮胎())
#在我的车上放一块火石
我的车轮胎(1,“火石”)
Tires
不是子类,它是一个嵌套类。这就引出了一个问题,Tires
为什么嵌套在Car
中?您运行发布的代码了吗<代码>汽车
类没有轮胎
属性…您不应该使用这样的嵌套类-只需将“类轮胎”定义拉到顶层,并在汽车类中具有正常的self.Tires=Tires()
属性。Python允许嵌套类,但这在O.O.编程中没有实际用途-它可以使用嵌套类作为名称空间,这是完全不同的事情。@Calab让我这样说,实际上从来没有一个好的理由使用嵌套类,所以不,它应该嵌套在Cars
中是没有意义的。如果您想将逻辑上相关的类/函数组合在一起,这就是模块的用途。