[英]Appending an object to a list with python is throwing object memory address instead of the data inside the object
I am getting an wierd output when I try to append an object created in the loop 当我尝试附加在循环中创建的对象时,我得到一个奇怪的输出
animals.txt animals.txt
ralph, dog, 3
buster, cat, 8
sammy, bird, 5
mac, dog, 1
coco, cat, 6
pet.py 宠物
class Pet:
# The __init__ method initializes the data attributes of the Profile class
def __init__(self, name ='', animal_type = '', age = ''):
self.__name = name
self.__animal_type = animal_type
self.age = 0
def __str__(self):
string = self.__name + ' ' + self.__animal_type + ' ' + str(self.age)
return string
def set_name(self, name):
self.__name = name
def get_name(self):
return self.__name
def set_animal_type(self, breed):
self.__animal_type = breed
def get_animal_type(self):
return self.__animal_type
def set_age(self, old):
self.age = old
def get_age(self):
return self.age
animals.py 动物
import pet
myPet = pet.Pet()
animals = []
infile = open("animals.txt", "r")
lines = infile.readlines()
for line in lines:
data = line.split(",")
myPet.set_name(data[0])
myPet.set_animal_type(data[1])
myPet.set_age(data[2])
# print (data[0], data[1], data[2])
print(myPet)
animals.append(myPet)
print(animals)
infile.close()
when I print the object created with each iteration with print(myPet) I get this; 当我使用print(myPet)打印每次迭代创建的对象时,会得到此信息;
ralph dog 3
拉尔夫犬3
buster cat 8
克星猫8
sammy bird 5
萨米鸟5
mac dog 1
mac狗1
coco cat 6
可可猫6
I then append the object myPet and this is the output repeated 5 times in the list when i print(animals) 然后我追加对象myPet,这是我打印(动物)时在列表中重复输出5次的结果
pet.Pet object at 0x00000185DCAE1128
pet.Pet对象位于0x00000185DCAE1128
I am not sure what is going on I have tried 我不确定我尝试了什么
myPet.set_name(data[0])
animals.append(myPet.get_name)
and 和
myPet.set_name(data[0])
name = myPet.get_name
animals.append(name)
giving the same error 给出相同的错误
bound method Pet.get_name of pet.Pet object at 0x000002C0F0BF6C88
pet.Pet对象的绑定方法Pet.get_name位于0x000002C0F0BF6C88
Changing your code so that you create a new Pet instance for every Pet, rather than changing the values of an existing Pet instance will mean you are appending the different pet values to your list. 更改代码,以便为每个Pet创建一个新的Pet实例,而不是更改现有Pet实例的值,这意味着您要将不同的pet值追加到列表中。
import pet
animals = []
infile = open("animals.txt", "r")
lines = infile.readlines()
for line in lines:
data = line.split(",")
myPet = pet.Pet()
myPet.set_name(data[0])
myPet.set_animal_type(data[1])
myPet.set_age(data[2])
# print (data[0], data[1], data[2])
print(myPet)
animals.append(myPet)
print(animals)
infile.close()
When you print
a single instance of MyPet
, Python calls MyPet
's __str__
method to get a string representation. 当您
print
MyPet
的单个实例时,Python会调用MyPet
的__str__
方法来获取字符串表示形式。
When you print a list of objects, Python displays the result of calling repr
on each element of the list. 当您打印对象列表时,Python将在列表的每个元素上显示调用
repr
的结果。 By default this will produce 默认情况下,这将产生
a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object
用尖括号括起来的字符串,其中包含对象类型的名称以及其他信息,通常包括对象的名称和地址
You can override this behaviour by adding a __repr__
method to MyPet
; 您可以通过添加一个覆盖此行为
__repr__
方法MyPet
; this code makes __str__
and __repr__
the same: 此代码使
__str__
和__repr__
相同:
class MyPet:
def __repr__(self):
string = self.__name + ' ' + self.__animal_type + ' ' + str(self.age)
return string
__str__ == __repr__
Conventionally, the output of __repr__
is 按照惯例,
__repr__
的输出是
a string that would yield an object with the same value when passed to eval()
传递给eval()时将产生具有相同值的对象的字符串
which you could do with this code: 您可以使用以下代码执行此操作:
class MyPet:
def __repr__(self):
return "MyPet(name='{}', animal_type='{}', age={})".format(self.__name,
self.__animal_type,
self.age)
def __str__(self):
string = self.__name + ' ' + self.__animal_type + ' ' + str(self.age)
return string
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.