Posts: 29
Threads: 14
Joined: Oct 2017
Nov-27-2017, 06:23 PM
(This post was last modified: Nov-27-2017, 06:23 PM by QueenSvetlana.)
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:
- If you wanted to keep an a list immutable, is this the best way to do it?, If not, how?
- 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.
Posts: 8,156
Threads: 160
Joined: Sep 2016
Nov-27-2017, 06:37 PM
(This post was last modified: Nov-27-2017, 06:37 PM by buran.)
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
Posts: 29
Threads: 14
Joined: Oct 2017
Nov-27-2017, 06:44 PM
(This post was last modified: Nov-27-2017, 06:44 PM by QueenSvetlana.)
(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.
Posts: 8,156
Threads: 160
Joined: Sep 2016
Nov-27-2017, 06:50 PM
(This post was last modified: Nov-27-2017, 06:54 PM by buran.)
(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)
Posts: 29
Threads: 14
Joined: Oct 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?
Posts: 8,156
Threads: 160
Joined: Sep 2016
Posts: 29
Threads: 14
Joined: Oct 2017
(Nov-27-2017, 07:04 PM)buran Wrote: https://opensource.com/article/17/4/grok-gil
what happened to the other link?
Posts: 8,156
Threads: 160
Joined: Sep 2016
I edited the post, because it's too old. but anyway - now I reverted it back
Posts: 3,458
Threads: 101
Joined: Sep 2016
(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/thread...ck-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())
Posts: 29
Threads: 14
Joined: Oct 2017
Nov-27-2017, 07:24 PM
(This post was last modified: Nov-27-2017, 07:25 PM by QueenSvetlana.)
(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/thread...ck-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  .
|