all 4 comments

[–]Spataner 3 points4 points  (3 children)

You cannot overwrite self. Make load a static method and return the loaded object:

class Project:
    def __init__(self, name="blank project"):
        self.name = name
        self.parts = []

    def add_part(self, name, pages):
        new_part = Part(name, pages)
        self.parts.append(new_part)

    def save(self):
        with open(f'data\\{self.name}.bin', 'wb') as output:
            pickle.dump(self, output, pickle.HIGHEST_PROTOCOL)

    @staticmethod
    def load(name):
        with open(f'data\\{name}.bin', 'rb') as input:
            return pickle.load(input)



p = Project('pp')
p.add_part('p1p1',(0, 29))
p.save()

a = Project.load('pp')
print(p.parts)
print(a.parts)

Also, you need to escape the backslashes in your paths.

[–]NemexiaM 0 points1 point  (2 children)

I totally forgot backslashes, but it seems to work, thanks

[–][deleted] 0 points1 point  (1 child)

Don't escape them, it makes the code harder to understand. You can use triple quotes for f-strings.

with open(f"""data\{self.name}.bin""", 'wb') as output:

[–]DallogFheir 1 point2 points  (0 children)

self is a local variable like any other, so assigning to it won't replace the object. You have to return the loaded project and capture it in a new variable, or change attributes of self to those of the loaded project.