Python 3: AttributeError

1

Hello, I am trying to make a very simple program that creates the data of a rectangle and finds its center:

class Rectangulo:
    pass
def encuentra_centro(box):
    p = Rectangulo()
    p.x = box.esquina.x + box.anchura / 2.0
    p.y = box.esquina.y + box.altura / 2.0
    return p
 def Nuevo_Rectangulo(an, al, x, y):
    rect = Rectangulo()
    rect.anchura = an
    rect.altura = al
    rect.esquina.x = x
    rect.esquina.y = y
    return rect
 caja = Nuevo_Rectangulo(200.0, 100.0, 4.0, 5.0)
 x = encuentra_centro(caja)

But when I run this script it throws me the following error:

AttributeError: 'Rectangulo' object has no attribute 'esquina'

Does anyone know what happened and how to fix it?

    
asked by MahaSaka 17.09.2017 в 01:43
source

1 answer

2

It throws you this error because in effect your Rectangle object does not have any 'corner' attribute, this is because your class is not implemented correctly.

The correct way to implement your rectangle class would be the following:

class Rectangulo(object):
    def __init__(self, an, al, x, y):
        self.an = an
        self.al = al
        self.x = x
        self.y = y
    def encuentra_centro(self):
        return(((self.x + self.an)/2), ((self.y + self.al)/2))

rect = Rectangulo(200.0, 100.0, 4.0, 5.0)
print(rect.encuentra_centro())

When you create a class the first thing you have to do is pass " object " as an argument (In doing this you are creating a sub-class of object, all classes are "sub- classes "of object but I will not go into this topic because it is not relevant and you can google it) .

The next thing is to use the method __init__(self): , this method is the first thing that is executed when instantiating a class and this is where the arguments that must be passed to the class are placed so that this can be instantiated.

The first parameter should always be " self " (it can be the word you want but self is the one commonly used) , followed by the arguments you choose .

To be able to access these arguments from within the class, you have to define them as follows self.argumento = argumento (these new variables are commonly named as the argument you are assigning to them, but you can name them as you like)

Since you defined your variables in __ init __ you can define the methods of your class the methods are defined as any function but you always have to pass them the self argument and if you spend more of a self argument should always be the first.

That's the correct way to implement a class.

To instantiate a class and use it you have to assign it to a variable rect = Rectangulo(200.0, 100.0, 4.0, 5.0) , when a class is instantiated you do not have to pass the argument self only those that you have defined.

The methods of the classes are used with the "point notation" clase.metodo() where class is the instance of a class rect.encuentra_centro()

This explanation is extremely basic and does not cover more than the surface of the subject of the classes and the object-oriented programming. I recommend this course , it's in English but you can Subtitle videos and it's free.

    
answered by 17.09.2017 / 03:07
source