missing 1 required positional argument - python

1

I am practicing with classes, objects and databases. I can not make this code work and I'm not sure what I'm doing wrong. I receive

TypeError: nuevo_articulo() missing 1 required positional argument: 'precio'

Here is my code:

import mysql.connector

class Manejo_stock:
    def __init__(self):
        self.conexion = mysql.connector.connect(host ="localhost",user="root", passwd="", database="articulos")
        self.cursor1= self.conexion.cursor() # abro un cursor para recorrer y enventualmente modificar los datos

    def nuevo_articulo(self,articulo, cantidad, precio):
        nuevo_articulo = articulo, precio, cantidad
        sql = 'insert into articulos (articulo, cantidad, precio) values (%s, %s, %s)'
        self.cursor1.execute(sql, nuevo_articulo)
        self.conexion.commit()
        self.conexion.close()
        return nuevo_articulo
        print("Se agregado correctamente el articulo")

articulo1 = Manejo_stock()
articulo1 = Manejo_stock.nuevo_articulo('peras', 5, 23.5)
    
asked by Diego Esperguin 16.12.2018 в 20:27
source

1 answer

1

You have some concept errors.

  • Within the nuevo_articulo() method, at the end, you have a print() after the return . Obviously it should be before, because once you do return the rest of the function will no longer be executed.
  • To invoke the nuevo_articulo() method, you must do it on an object , and not on the class. That is what is failing you because when you do objeto.metodo(parametros) , Python translates it to Clase.metodo(objeto, parametros) , causing the object in question to become self within the method. When calling it as Clase.metodo(parametros) , which is what you have done, a parameter in the call would be missing (python can not know which and assumes that it is the last one, precio , but in reality it was the first, self ).
  • When you "invoke" a class, so Clase() , its constructor is executed and an object of that class is returned. In your case, since the class is called Manejo_stock , the created object could be called manejador , instead of articulo1 , because it is not an article. This makes the code more understandable.

Thus, it would be in the following form:

import mysql.connector

class Manejo_stock:
    def __init__(self):
        self.conexion = mysql.connector.connect(host ="localhost",user="root", passwd="", database="articulos")
        self.cursor1= self.conexion.cursor() # abro un cursor para recorrer y enventualmente modificar los datos

    def nuevo_articulo(self,articulo, cantidad, precio):
        nuevo_articulo = articulo, precio, cantidad
        sql = 'insert into articulos (articulo, cantidad, precio) values (%s, %s, %s)'
        self.cursor1.execute(sql, nuevo_articulo)
        self.conexion.commit()
        self.conexion.close()
        print("Se agregado correctamente el articulo")
        return nuevo_articulo


manejador = Manejo_stock()
articulo1 = manejador.nuevo_articulo('peras', 5, 23.5)
    
answered by 16.12.2018 / 20:38
source