[英]Python: Can't copy pygame surface objects
I have a class: 我有一堂课:
class personInfo:
def __init__(self,name,age,height,hairColour,face):
self.name = name
self.age = age
self.height = height
self.hairColour = hairColour
self.face = face
I have several images here that I load in using the pygame module. 我在这里使用pygame模块加载了几张图片。
yellowFace = pygame.image.load("happy.png")
blueFace = pygame.image.load("sad.png")
redFace = pygame.image.load("angry.png")
I created an array that holds instances of that class. 我创建了一个包含该类实例的数组。 Here I am populating it. 我在这里填充它。
personArray = []
while len(array) != 10:
personArray.append(personClass("John",32,6,"Blond",yellowFace))
I would like to store the actual variables of that class (name, age height,hairColour, etc) in a variable I will call "personStorage". 我想将该类的实际变量(名称,年龄,身高,hairColour等)存储在一个变量中,我将其称为“ personStorage”。 However, I can't have that var be mutatable since I need to access that var's values and change them. 但是,我不能让该变量可变,因为我需要访问该变量的值并更改它们。 Doing this can't change the values of any instances inside the personArray. 这样做不能更改personArray内部任何实例的值。 How would I do this? 我该怎么做?
EDIT: I also can't seem to be able to Copy this class because I get a type error that says: "can't pickle pygame.surface objects" since I have a value of a surface object within that class. 编辑:我似乎也无法复制此类,因为我收到一个类型错误,说:“不能腌制pygame.surface对象”,因为我在该类中有一个Surface对象的值。
If I understood what you're trying to do : 如果我了解您要执行的操作:
class PersonInfo:
def __init__(self,name,age,height,hairColour):
self.name = name
self.age = age
self.height = height
self.hairColour = hairColour
def toList(self):
return [self.name, self.age, self.height, self.hairColour]
By the way, class names' first letter is always uppercase. 顺便说一句,类名的首字母始终是大写。
EDIT : To achieve what you're trying to do : 编辑:要实现您正在尝试做的事情:
old = PersonInfo("old", 1, 2, "blue")
new = PersonInfo(*old.toList())
new.name = "new"
print(old.name) #'old'
I have created setters and getters to get the data.Also you can create a copy of the instance that holds the data. 我创建了setter和getter来获取数据。此外,您还可以创建保存数据的实例的副本。
from copy import deepcopy
class PersonInfo:
def __init__(self,name,age,height,hairColour):
self.name = name
self.age = age
self.height = height
self.hairColour = hairColour
x = PersonInfo("Mario",34,1.70,"blue")
print(x.height)//prints 1.70
x1 = deepcopy(x)
print(x1.age)
Use the copy module: 使用复制模块:
The function copy() shallow copy operation on arbitrary Python objects. 函数copy()对任意Python对象进行浅表复制操作。
import copy
class PersonInfo:
def __init__(self, name, age, height, hair_colour):
self.name = name
self.age = age
self.height = height
self.hairColour = hair_colour
personArray = []
for x in range(20, 24):
personArray.append(PersonInfo("John", x, 6, "Brown"))
personStorage = copy.copy(personArray[2])
personStorage.name = "Ana"
print("\rpersonArray[2].name is %s\n"
"\rpersonStorage.name is %s"
% (personArray[2].name, personStorage.name))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.