我创建了一个类来保存有关我的数据点的所有信息。数据点通常在一系列测量中收集,因此我想将它们放在一起:因此,DataPoints
和 LotsOfDataPoints
#this is simplified code, but reproduces my issue
class DataPoint:
def __init__(self, value):
self.value = value
def add_number(self, number):
self.value += number
def __repr__(self):
return str(self.value)
class LotsOfDataPoints:
def __init__ (self, *values):
self.lst = []
for value in values:
self.lst.append(DataPoint(value))
def add_one(self):
self.lst = [d.add_number(1) for d in self.lst]
我可以成功创建一个数据点:
d = DataPoint(1)
print(d)
> 1
d.add_number(1)
print(d)
> 2
我可以成功制作lotsOfDataPoints:
ds = LotsOfDataPoints(1,2,3,4,5)
print(ds.lst)
> [1, 2, 3, 4, 5]
我可以在单个 DataPoint 上调用 DataPoint 方法
ds.lst[0].add_number(1)
print(ds.lst)
> [2, 2, 3, 4, 5]
但是,当我尝试 add_one()
到我的 lotOfDataPoints 时,它们都转向 None
。
ds.add_one()
print(ds.lst)
> [None, None, None, None, None]
我在这里做错了什么?
答案 0 :(得分:1)
我想说问题出在这里
def add_one(self):
self.lst = [d.add_number(1) for d in self.lst]
您的 DataPoint.add_number
方法不返回任何内容,因此 self.lst
填充了 None
值。你真正想做的是
def add_one(self):
for d in self.lst:
d.add_number(1)
这应该在不清空 DataPoint
属性的情况下编辑 LotsOfDataPoints.lst
对象。
答案 1 :(得分:1)
这可以通过在函数 public class AssemblyViewModel : BaseVM
{
public double ScreenWidth => Xamarin.Forms.Application.Current.MainPage.Width;
#region Case
bool caseIsVisible;
public bool CaseIsVisible
{
get => caseIsVisible;
set => SetProperty(ref caseIsVisible, value);
}
public string CaseImgSource { get; }
public string CaseLabel { get; }
#endregion
#region CaseCover
bool caseCoverIsVisible;
public bool CaseCoverIsVisible
{
get => caseCoverIsVisible;
set => SetProperty(ref caseCoverIsVisible, value);
}
public string CaseCoverImgSource { get; }
public string CaseCoverLabel { get; }
#endregion
#region HardDiskDrive
bool hardDiskDriveIsVisible;
public bool HardDiskDriveIsVisible
{
get => hardDiskDriveIsVisible;
set => SetProperty(ref hardDiskDriveIsVisible, value);
}
public string HardDiskDriveImgSource { get; }
public string HardDiskDriveLabel { get; }
#endregion
#region CaseScrew
bool caseScrewIsVisible;
public bool CaseScrewIsVisible
{
get => caseScrewIsVisible;
set => SetProperty(ref caseScrewIsVisible, value);
}
public string CaseScrewImgSource { get; }
public string CaseScrewLabel { get; }
#endregion
#region Heatsink
bool heatsinkIsVisible;
public bool HeatsinkIsVisible
{
get => heatsinkIsVisible;
set => SetProperty(ref heatsinkIsVisible, value);
}
public string HeatsinkImgSource { get; }
public string HeatsinkLabel { get; }
#endregion
#region MemoryModule
bool memoryModuleIsVisible;
public bool MemoryModuleIsVisible
{
get => memoryModuleIsVisible;
set => SetProperty(ref memoryModuleIsVisible, value);
}
public string MemoryModuleImgSource { get; }
public string MemoryModuleLabel { get; }
#endregion
#region Motherboard
bool motherboardIsVisible;
public bool MotherboardIsVisible
{
get => motherboardIsVisible;
set => SetProperty(ref motherboardIsVisible, value);
}
public string MotherboardImgSource { get; }
public string MotherboardLabel { get; }
#endregion
#region MotherboardScrew
bool motherboardScrewIsVisible;
public bool MotherboardScrewIsVisible
{
get => motherboardScrewIsVisible;
set => SetProperty(ref motherboardScrewIsVisible, value);
}
public string MotherboardScrewImgSource { get; }
public string MotherboardScrewLabel { get; }
#endregion
#region PowerSupply
bool powerSupplyIsVisible;
public bool PowerSupplyIsVisible
{
get => powerSupplyIsVisible;
set => SetProperty(ref powerSupplyIsVisible, value);
}
public string PowerSupplyImgSource { get; }
public string PowerSupplyLabel { get; }
#endregion
#region Processor
bool processorIsVisible;
public bool ProcessorIsVisible
{
get => processorIsVisible;
set => SetProperty(ref processorIsVisible, value);
}
public string ProcessorImgSource { get; }
public string ProcessorLabel { get; }
#endregion
public AssemblyViewModel()
{
CaseIsVisible = true;
CaseImgSource = "img_assembly_case.png";
CaseLabel = "Case";
CaseCoverIsVisible = true;
CaseCoverImgSource = "img_assembly_case_cover.png";
CaseCoverLabel = "Case Cover";
CaseScrewIsVisible = true;
CaseScrewImgSource = "img_assembly_case_screw.png";
CaseScrewLabel = "Case Screw";
HardDiskDriveIsVisible = true;
HardDiskDriveImgSource = "img_assembly_hard_disk_drive.png";
HardDiskDriveLabel = "Hard Disk Drive";
HeatsinkIsVisible = true;
HeatsinkImgSource = "img_assembly_heat_sink.png";
HeatsinkLabel = "Heatsink";
MemoryModuleIsVisible = true;
MemoryModuleImgSource = "img_assembly_memory_module.png";
MemoryModuleLabel = "Memory Module";
MotherboardIsVisible = true;
MotherboardImgSource = "img_assembly_motherboard.png";
MotherboardLabel = "Motherboard";
MotherboardScrewIsVisible = true;
MotherboardScrewImgSource = "img_assembly_motherboard_screw.png";
MotherboardScrewLabel = "Motherboard Screw";
PowerSupplyIsVisible = true;
PowerSupplyImgSource = "img_assembly_power_supply.png";
PowerSupplyLabel = "Power Supply";
ProcessorIsVisible = true;
ProcessorImgSource = "img_assembly_processor.png";
ProcessorLabel = "Processor";
}
}
中返回 self.value
来解决。因为,在循环 - add_number
中,函数刚刚被调用。不保存这些值。修改后的代码如下:
[d.add_number(1) for d in self.lst]
得到的输出如下:
class DataPoint:
def __init__(self, value):
self.value = value
def add_number(self, number):
self.value += number
return self.value
def __repr__(self):
return str(self.value)
class LotsOfDataPoints:
def __init__ (self, *values):
self.lst = []
for value in values:
self.lst.append(DataPoint(value))
def add_one(self):
self.lst = [d.add_number(1) for d in self.lst]
d = DataPoint(1)
print(d)
> 1
ds = LotsOfDataPoints(1,2,3,4,5)
print(ds.lst)
> [1, 2, 3, 4, 5]