python - 关于用括号调用类的实例
问题描述
我正在做关于制作模块的作业,在过去的 5 个小时里我一直在努力解决这个问题,我需要帮助来理解这一点。
我必须制作几个以某种方式交互的类,这是我到目前为止所做的代码。
#----------------------------------------
#This class makes an element to put inside a piece of the block
class Element:
def __init__(self, tipus, alcada):
self.tipus = tipus
self.alcada = alcada
#---------------------------------------------------------------
#This class makes a block separated in "ncossos" number of pieces
class MobleModular:
alt = 0
ample = 0
ncossos = 0
def __init__(self, alt, ample, ncossos):
global dict
self.alt = alt
self.ample = ample
self.ncossos = ncossos
#-------------------------------------
#This is the only way i know of making a database for each piece of the block
dict = {}
for x in range(self.ncossos):
dict[x] = []
#This returns {0: [], 1: [], 2: [], 3: []} using number 4 in "ncossos" at __init__
然后我必须使用 Element() 创建用于存储在每个列表中的元素,所以我做了这个
def afegir(self, nc, tipus, alcada):
self.nc = nc #I access the dictionary with this number
self.tipus = tipus
self.alcada = alcada
y = Element(tipus, alcada)
dict[nc].append(y)
#--------------------------------
#Then i enter the following values
m.afegir(0, 'A', 90)
m.afegir(0, 'C', 20)
m.afegir(0, 'C', 20)
m.afegir(0, 'C', 30)
m.afegir(1, 'A', 90)
m.afegir(1, 'A', 30)
m.afegir(1, 'C', 20)
m.afegir(1, 'C', 30)
m.afegir(2, 'B', 30)
m.afegir(2, 'B', 30)
m.afegir(2, 'B', 30)
m.afegir(2, 'B', 40)
m.afegir(2, 'C', 30)
m.afegir(3, 'B', 60)
m.afegir(3, 'A', 70)
m.afegir(3, 'C', 30)
现在我的列表充满了对象,没关系,因为我可以调用“dict[0][0].tipus/alcada”来获取对象值
这就是问题开始的地方,练习要求我做这个,我会展示它,因为我不知道如何解释它
x = m[1, 3]
#The first number being the dictionary position and the second number being the nested list position,
#so it returns the THIRD element from the FIRST piece of the block
#And when i call:
x.tipus, x.alcada
#It has to return:
('C', 20)
我到底是怎么做到的?我需要用括号调用我的实例,然后将位置的对象分配给一个新变量
解决方案
您应该简化逻辑以使编码更容易。创建“数据库”时,只需将数据存储为元组。检索特定索引时,创建一个新的 Element 对象并返回它。
试试这个代码:
class Element:
def __init__(self, tipus, alcada):
self.tipus = tipus
self.alcada = alcada
class MobleModular:
def __init__(self):
self.lst = [] # list of tuples
def afegir(self, nc, tipus, alcada):
self.lst.append((nc, tipus, alcada)) # add tuple
def __getitem__(self, idx): # for brackets
lst = [x for x in self.lst if x[0] == idx[0]] # find tuples by first index
e = lst[idx[1]-1] # get specific tuple
return Element(e[1],e[2]) # return new element object
m=MobleModular()
m.afegir(0, 'A', 90)
m.afegir(0, 'C', 20)
m.afegir(0, 'C', 20)
m.afegir(0, 'C', 30)
m.afegir(1, 'A', 90)
m.afegir(1, 'A', 30)
m.afegir(1, 'C', 20)
m.afegir(1, 'C', 30)
m.afegir(2, 'B', 30)
m.afegir(2, 'B', 30)
m.afegir(2, 'B', 30)
m.afegir(2, 'B', 40)
m.afegir(2, 'C', 30)
m.afegir(3, 'B', 60)
m.afegir(3, 'A', 70)
m.afegir(3, 'C', 30)
x = m[1, 3]
print((x.tipus, x.alcada)) # ('C', 20)
推荐阅读
- r - 在 r 中同时旋转数据框并计算分类观察值
- reactjs - React / Redux - 直接到其他路线时丢失状态
- sql - 出现错误:子查询返回超过 1 个值
- jquery - 如何在同一个父母下找到一个元素
- r - 如何将 dplyr 操作与列名的字符串列表一起使用
- java - gremlin - 用作图形遍历初始化的最佳步骤
- android - 如果我在自定义视图中访问方法,应用程序崩溃
- javascript - 如何找到带有 JS 的网页声明?
- python - 纠正不完整的年份 - Python
- c# - 配置 IOptions
当 T 为 Type 时使用 ServiceCollection