Search code examples
pythonclassmoduleinstancebrackets

About calling an instance of a class with brackets


I'm doing homework that is about making a module and i've been REALLY struggling with this for the past 5 hours, and i need help understanding this.

I have to make a couple of classes that somehow interact, this is the code i've made so far.

#----------------------------------------
#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__

Then i have to use Element() to create elements for storing in each list so i made this

    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)

Now my lists are full of objects, that's okay since i can call "dict[0][0].tipus/alcada" to get the object values

And here is where the problem starts, the exercise asks me to make this, i'm gonna show it since I don't know how to explain it

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)

How the hell in the world do i do this? I need to call my instance with brackets and then assign the position's object to a new variable


Solution

  • You should simplify the logic to make the coding easier.. When creating the 'database', just store the data as tuples. When retrieving a specific index, create a new Element object and return it.

    Try this code:

    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)