I am trying to get the user to make an object called a NoteSet, each NoteSet will be put into a global list called db. This is my attempt at making this:
import sys
import datetime
db = list()
class NoteSet:
nextseqNum = 0
def __init__(self,name,description,hidden):
global db
self.seqNum = Note.nextseqNum
self.name = name
self.description = description
self.dateCreated = datetime.date.today()
self.hidden = hidden
self.notes = list()
db[self.seqNum] = self
print(self)
print(len(db))
Note.nextseqNum += 1
When I try to create an object for example:
NoteSet('example','ex',True)
It gives me the error
Traceback (most recent call last): File "", line 1, in NoteSet('example','ex',True) File "C:\\Users\\Brandon\\Desktop\\step5.py", line 22, in init db[self.seqNum] = self IndexError: list assignment index out of range
Is this the right way to make a global list of objects?
You get this error because db
has no elements in it (python lists are initialized to length 0), so when you try and replace the element at location self.seqNum
, you are acessing an invalid index. It has nothing to do with the global-ness of it.
If we assume that this global list is only ever going to be accessed in a thread-safe manner, you should simply be able to do:
db.appened(self)
Instead. However, as mentioned in the comments, it makes more sense in this use case to make db
a class variable if this class is the 'gate keeper' to interfacing with the db list.
UPDATE
To address the OP's question in the comments,
I am looking to be able to keep track of the location of the objects in the list by the seqNum
As currently written, seqNum
will always increment linearly, forever, with each new NoteSet
instance. If we assume thread-safe access of Note.nextseqNum
, then what you're trying to do via db[self.seqNum]
is already implicitly done via db.append(self)
, because len(db) == Note.nextseqNum
, always. For now, we're going to ignore what happens if you cand remove elements from db
, because right now your system doesn't account for that at all and would completely break anyway.
If, however, in the future seqNum
doesn't just increase monotonically forever each time you make a new instance, you can simply make db
a dict
instead of a list
:
db = dict()
And then insert the new instance to it exactly as you are currently,
db[self.seqNum] = self
db
now represents a mapping of a seqNum
to a NoteSet
explicitly, rather than an implicit relationship based on an array index.
I would actually recommend doing it this way anyway, as it will also solve the problem of removing items from db
for 'free'. As is, doing del db[instance.seqNum]
will completely invalidate all mappings of seqNum
into db
for any instance that came after the removed instance. But if db
is a dict
, then this operation does what you expect it to and all of the seqNum
values still map to the correct instance in db
.
So, to bring it all together, I would recommend you alter your class to look like the following:
import sys
import datetime
class NoteSet:
nextseqNum = 0
db = dict()
def __init__(self,name,description,hidden):
self.seqNum = NoteSet.nextseqNum
self.name = name
self.description = description
self.dateCreated = datetime.date.today()
self.hidden = hidden
self.notes = list()
NoteSet.db[self.seqNum] = self
print(self)
print(len(db))
NoteSet.nextseqNum += 1
As @aruisdante said you will need to append to the list
Try this:
db = []
class ListObj:
def __init__(self, name, msg, hide=False):
self.name = name
self.msg = msg
self.hide = hide
db.append(self)
Good Luck!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.