I am having some issues concerning the interaction between functions, methods and have a uncertainty of how to utilize the use of the built-in function __str__
.
To start this of, I have a class called vara, which looks like:
class vara(object):
def __init__(self, kod, namn, pris, butikantal):
self.kod = kod
self.namn = namn
self.pris = pris
self.antal = butikantal
and i have a function that creats a list of object for the vara klass, that looks like:
def mina_varor():
varor_fran_fil = open("varor.txt", "r")
varulista = []
for rad in varor_fran_fil:
varje_vara1 = rad
varje_vara2 = varje_vara1.split("/")
varorna = vara(varje_vara2[0], varje_vara2[1], varje_vara2[2], varje_vara2[3])
varulista.append(varorna)
return(varulista)
Now i want to be able to access a singel object in this list by simply typing then "kod" for the object. But I cant find any "kod" in my list. It seemed strange, so i tried printning the list, and got that:
[<__main__.vara object at 0x00000000031503C8>, <__main__.vara object at 0x00000000031507F0>, <__main__.vara object at 0x0000000003150710>, <__main__.vara object at 0x00000000031502B0>, <__main__.vara object at 0x00000000031505F8>, <__main__.vara object at 0x00000000031504E0>]
i looked it up, and it seems that python cant decide how to interpret my list. I think i need an __str__
method to do so, but how should i make my __str__
method look like if i want it to print something like:
Name: Hat
Price: 150
Quantity: 100
Code: 223
?