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.

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". 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. 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.

3 Answers

1
A.N. O'Nyme On Best Solutions

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'
0
Marios Nikolaou On

I have created setters and getters to get the data.Also you can create a copy of the instance that holds the data.

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)
0
Community On

Use the copy module:

The function copy() shallow copy operation on arbitrary Python objects.

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))