JB_
JB_

Reputation: 583

Return tuple list in class

How make for return tuple in class

class Product:
    def __init__(self, name, value, image):
        self.name = name
        self.value = value
        self.image = image

    def __iter__(self):
        return (self.name, self.value, self.image)

I to need make insert in MySql witch executemany, how are no interator Does not work.

Implements class:

from Product import Product

Products = []

Product_name = "Fruit 01"
Product_price = 12.25
Product_image = "src/test.png"

Products.append(Product(
    Product_name,
    Product_price,
    Product_image
))

Upvotes: 3

Views: 1998

Answers (1)

blhsing
blhsing

Reputation: 106523

The __iter__ method should return an iterator.

You can either use the iter function to create an iterator from the tuple:

def __iter__(self):
    return iter((self.name, self.value, self.image))

or use the yield from statement to achieve the same:

def __iter__(self):
    yield from (self.name, self.value, self.image)

Upvotes: 2

Related Questions