Ваш класс демонстрирует, что вы новичок в Python, поэтому я постараюсь очистить его и указать на некоторые вещи, не переписывая его полностью. Другие решения здесь более чистые, но, надеюсь, это поможет вам понять некоторые концепции.
class chemistry:
# Because this is a class method, it will automatically
# receive a reference to the instance when you call the
# method. You have to account for that when declaring
# the method, and the standard name for it is `self`
def readAMU(self):
infil = open("AtomAMU.txt", "r")
line = infil.readline()
Atoms = list()
# As Frédéric said, tuples are immutable (which means
# they can't be changed). This means that an empty tuple
# can never be added to later in the program. Therefore,
# you can leave the next line out.
# Element = ()
while line !="":
line = line.rstrip("\n")
parts = line.split(" ");
element = parts[0]
AMU = parts[1]
# The next several lines indicate that you've got the
# right idea, but you've got the method calls reversed.
# If this were to work, you would want to reverse which
# object's `append()` method was getting called.
#
# Original:
# element.append(Element)
# AMU.append(Element)
# Element.append(Atoms)
#
# Correct:
Element = (element, AMU)
Atoms.append(Element)
# If you don't make sure that `Atoms` is a part of `self`,
# all of the work will disappear at the end of the method
# and you won't be able to do anything with it later!
self.Atoms = Atoms
Теперь, когда вы хотите загрузить атомные номера, вы можете создать экземпляр класса chemistry
и вызвать его метод readAMU()
!
>>> c = chemistry()
>>> c.readAMU()
>>> print c.Atoms
Помните, Atoms
является частью экземпляра c
из-за последней строки: self.Atoms = Atoms
.