0

When calling on the class in order to return the dictionary, the values of the dictionary are the location of the object in the memory, which is not what I need.an image of what I am recieving

    class Element:

def __init__(self,name,number,symbol,weight):
    self.__name = name
    self.__number = number
    self.__symbol = symbol
    self.__weight = weight

def getname(self):
    return self.__name   

def getnumber(self):
    return self.__number

def getsymbol(self):
    return self.__symbol

def getweight(self):
    return self.__weight

def __str__(self):
    return self.__name + ' ' + str(self.__number) + ' ' + self.__symbol + ' ' + str(self.__weight)


    class PeriodicTable():

def __init__(self,file):
    lines = open(file,"r").read().split("\n")
    self.ElementsDictionary = {}
    for line in lines[1:]: 
        tokens = line.split(",")
        name, number, symbol, weight = tokens[0], tokens[1], tokens[2], tokens[3]
        self.ElementsDictionary[symbol] = Element(name,number,symbol,weight)

def __str__(self):
    return str(self.ElementsDictionary)

x = PeriodicTable("elements.txt")
print(x)
Tarek AS
  • 187
  • 13
  • the problem is in this line i think : ( self.ElementsDictionary[symbol] = Element(name,number,symbol,weight) ) – Tarek AS Dec 10 '17 at 15:06
  • You need to add an implementation for `__repr__`: `def __repr__(self): return str(self)` – DeepSpace Dec 10 '17 at 15:07
  • Possible duplicate of [Difference between \_\_str\_\_ and \_\_repr\_\_ in Python](https://stackoverflow.com/questions/1436703/difference-between-str-and-repr-in-python) – DeepSpace Dec 10 '17 at 15:09

1 Answers1

0

Looks like an issue in this line.

self.ElementsDictionary[symbol] = Element(name,number,symbol,weight)

Here, store the object in string format it will solve your problem

self.ElementsDictionary[symbol] = str(Element(name,number,symbol,weight))
nj2237
  • 1,220
  • 3
  • 21
  • 25