Inheritance in the classes

2

I have a question about the classes, is it about if having two classes (a father and another daughter) could get a list (global) that is in the father and join new elements from the daughter?

And if that is how I could do it, then I have managed to access the list I wanted but when it comes to joining elements to that list, the ones that had already appeared do not appear and I do not know if it is because I really take the list I want or because when joining elements in some way the previous ones are eliminated. The doubt arose as a result of this:

class Galapago:

    tortugas=[]
    l_line=drawlib.make_line_list()
    l_sprite= drawlib.make_sprite_list()

    def __init__(self):
        drawlib.start_render()
        sprite=drawlib.make_sprite("/home/Documentos/Proyecto2/ship (6).png",0.5)
        drawlib.set_center_x(sprite,0)
        drawlib.set_center_y(sprite,0)
        drawlib.set_angle(sprite,90)
        drawlib.finish_render()
        self.x= 0
        self.y= 0
        self.angle= 90
        self.color= colors.BLACK
        self.widht= 1
        self.sprite=sprite
        self.pen= 1 #con el 1 pintará con el 0 no hara nada
        Galapago.l_sprite.append(self.sprite)

the daughter class would be:

class Radio_Galapago(galapagoo.Galapago):

    def __init__(self):
        drawlib.start_render()
        self.sprite=drawlib.make_sprite("/home/Documentos/Proyecto2/microbit.png",0.1)
        n= random.randint(0,768)
        x=drawlib.set_center_x(self.sprite,n)
        y=drawlib.set_center_y(self.sprite,n)
        drawlib.set_angle(self.sprite,0)
        drawlib.draw_sprite(self.sprite)
        drawlib.finish_render()
        galapagoo.Galapago.l_sprite.append(self.sprite)

When I do this if I send print(l_sprite) I only see one element The code that I am trying to create from these two classes and that is therefore giving me error is the following:

import Radio_Galapago
import galapagoo
import drawlib
import colors

drawlib.open_window(1024, 768, "Prueba Radio_Galapago")
drawlib.set_background_color(colors.OCEAN_BOAT_BLUE)



alex=galapagoo.Galapago()
microbit=Radio_Galapago.Radio_Galapago()
senal=microbit.power_ratio(alex)
print(galapagoo.Galapago.l_sprite)
print(Galapago.l_sprite)

def pintar(tortuga, microbit):
    drawlib.start_render()
    drawlib.draw_list(Galapago.l_sprite)
    drawlib.finish_render()
    drawlib.pause(5)

"""def buscar(self,busca_senal):
    drawlib.start_render()
    drawlib.draw_list(pintar)
    busca_senal.forward(100)
    while senal < (-50):
        drawlib.start_render()
        drawlib.draw_list(pintar)
        senal_1= self.power_ratio(busca_senal)
        drawlib.finish_render()
        if senal_1 < senal:
            drawlib.start_render()
            drawlib.draw_list(pintar)
            busca_senal.forward(100)
            drawlib.finish_render()
        elif self.power_ratio(busca_senal)> senal_1:
            busca_senal.right(25)
            busca_senals.forward(100)
    drawlib.finish_render()
    drawlib.pause(3)"""


alex.penup()
pintar(alex,microbit)
#buscar(microbit, alex)
    
asked by airun 30.12.2018 в 20:49
source

1 answer

0

If I did not misunderstand your comments, what you are looking for is that by instantiating objects of the class Galapago or its descendant Radio_Galapago , elements are added to the list that is an attribute of the base class.

In the end, therefore, I understand that this list is a kind of "global variable" at the end of the day.

The following mini-snippet of code reduces to the minimum expression what I think you're trying. It works correctly:

class A:
  lista = []
  def __init__(self, v=0):
    A.lista.append(v)


class B(A):
  def __init__(self, v=1):
    A.lista.append(v)

A(); A(2); B(); A()
print(A.lista)
[0, 2, 1, 0]

Assuming that, in your code of the question galapagoo.Galapago is the same kind Galapago that you show at the beginning, everything should work, because it is equivalent to the minimum version that I just put.

That it does not work means that something you're not showing is not how it's supposed to be. It can be:

  • That galapagoo.Galapago is not the same as Galapago
  • That the class Galapago be defined twice (and each time its list is re-created)
  • Some detail in the instantiation of objects (?)
  • That the list you are trying to print with the print() is not Galapago.l_sprite
answered by 31.12.2018 / 11:29
source