Python Forum
Immutable Book Class - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Immutable Book Class (/thread-6547.html)

Pages: 1 2


Immutable Book Class - QueenSvetlana - Nov-27-2017

from copy import deepcopy


class Book:

    def __init__(self, book_title, authors):
        self.book_title = book_title
        self.authors = deepcopy(authors)
        self.logs = []

    def update_logs(self, log):
        self.logs.append(log)[b][/b]
I have a Book class, with a self.logs = [], the utility I'm creating helps people keep notes of the book they are currently reading. Now for learning purposes, I intend to keep this class immutable, it's meant for a book that the user bought, so I don't see how it could change(updating and re-printing the book is another matter, for my simple program, it's not a problem).

When modelling data that is really mutable(in my case, the logs), I've seen patterns where it updates the mutable data and returns an object reflecting the change.

In my case, it's only the list that needs updating, how would I go about keeping the list immutable?

What I tried:

class Book:

    def __init__(self, book_title, authors):
        self.book_title = book_title
        self.authors = deepcopy(authors)
        self._internal_logs = []
        self.logs = []

    @property
    def get_logs(self):
        return deepcopy(self._internal_logs)

    def update_logs(self, log):
        self.logs.append(log)
        self._internal_logs = deepcopy(self.logs)
Questions:
  1. If you wanted to keep an a list immutable, is this the best way to do it?, If not, how?

  2. When creating the list(self.logs = []), I do so in the constructor, the client code doesn't pass a list(IMO, they shouldn't, because no logs have been written yet), is this considered bad practice? Should I allow them to pass in an empty list? For example, the user may create a book file, but they haven't started reading the book yet. This means a book object is created without the logs.



RE: Immutable Book Class - buran - Nov-27-2017

why not use tuple, instead of list, if you want it immutable? Note that I don't think logs should be immutable - user may want to edit an old log


RE: Immutable Book Class - QueenSvetlana - Nov-27-2017

(Nov-27-2017, 06:37 PM)buran Wrote: why not use tuple, instead of list, if you want it immutable? Note that I don't think logs should be immutable - user may want to edit an old log

I wasn't thinking, I should have thought of tuple!

That being said, how would go about modelling the logs while keeping it thread safe? The data changes, as we both pointed out.


RE: Immutable Book Class - buran - Nov-27-2017

(Nov-27-2017, 06:44 PM)QueenSvetlana Wrote: That being said, how would go about modelling the logs while keeping it thread safe?
from your description I don't see where/why multi-thread approach will be used. Also I don't see how you plan to store the book data, incl. logs. I would expect a log would be a class on its own with properties like id, time created/last edit, text, book it refers to, etc.. and logs in Book class would be much better as dict (and mutable)


RE: Immutable Book Class - QueenSvetlana - Nov-27-2017

(Nov-27-2017, 06:50 PM)buran Wrote:
(Nov-27-2017, 06:44 PM)QueenSvetlana Wrote: That being said, how would go about modelling the logs while keeping it thread safe?
from your description I don't see where/why multi-thread approach will be used

This might not be a great example of when to use multi-threading, but suppose if you had to model data that changes but wanted to keep it thread-safe how would you do it?


RE: Immutable Book Class - buran - Nov-27-2017

https://opensource.com/article/17/4/grok-gil
http://effbot.org/zone/thread-synchronization.htm


RE: Immutable Book Class - QueenSvetlana - Nov-27-2017

(Nov-27-2017, 07:04 PM)buran Wrote: https://opensource.com/article/17/4/grok-gil

what happened to the other link?


RE: Immutable Book Class - buran - Nov-27-2017

I edited the post, because it's too old. but anyway - now I reverted it back


RE: Immutable Book Class - nilamo - Nov-27-2017

(Nov-27-2017, 06:53 PM)QueenSvetlana Wrote: but suppose if you had to model data that changes but wanted to keep it thread-safe how would you do it? 

With a lock. https://docs.python.org/3/library/threading.html#lock-objects

import threading

class Data:
    def __init__(self, value):
        self.value = value
        self.lock = threading.Lock()

    def get(self):
        with self.lock:
            return self.value

    def set(self, new_value):
        with self.lock:
            self.value = new_value

spam = Data("eggs")
print(spam.get())
spam.set("foo bar")
print(spam.get())



RE: Immutable Book Class - QueenSvetlana - Nov-27-2017

(Nov-27-2017, 07:19 PM)nilamo Wrote:
(Nov-27-2017, 06:53 PM)QueenSvetlana Wrote: but suppose if you had to model data that changes but wanted to keep it thread-safe how would you do it? 

With a lock. https://docs.python.org/3/library/threading.html#lock-objects

import threading

class Data:
    def __init__(self, value):
        self.value = value
        self.lock = threading.Lock()

    def get(self):
        with self.lock:
            return self.value

    def set(self, new_value):
        with self.lock:
            self.value = new_value

spam = Data("eggs")
print(spam.get())
spam.set("foo bar")
print(spam.get())

@ buran as well, Thanks for the help Smile.