| | |
Python nested objects, best practices.
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2007
Posts: 6
Reputation:
Solved Threads: 0
python Syntax (Toggle Plain Text)
class Item: def __init__(self, id, content, userid, duedate, collapsed, inhistory, priority, itemorder, faded, projectid, checked, datestring): self.id = id self.content = content self.userid = userid self.duedate = duedate self.collapsed = collapsed self.inhistory = inhistory self.priority = priority self.itemorder = itemorder self.faded = faded self.projectid = projectid self.checked = checked self.datestring = datestring def __str__(self): return self.content class Project: def __init__(self, id, name, userid, color, collapsed, itemorder, indent, order): self.id = id self.name = name self.userid = userid self.color = color self.collapsed = collapsed self.itemorder = itemorder self.indent = indent self.order = order self.items = {} def __str__(self): return self.name def __name__(self): return 'Project' def addItem(self,item): self.items.update({item.id:item}) def delItem(self,item): del self.items[item.id] class TodoList: def __init__(self): self.projects = {} def addProject(self,project): self.projects.update({project.id:project}) def delProject(self,project): del self.projects[project.id] def addItem(self,item): self.projects[item.projectid].addItem(item) def delItem(self,item): self.projects[item.projectid].delItem(item)
Some of the above code is changing as we speak but I am curious if I should make the projects dict and items dicts private and use accessor functions to move items between projects or change the attributes of a project or item.
Another thought is to have items and projects both be members of TodoList as I am provided with the Item's project ID when I get the Item, so I could just have a method that returned all the items with a given project ID and not bother with separating them into the individual projects.
I am just trying to design the best I can as I plan on using this app as an example of my Python coding ability, though I just started with python about 3 days ago.
Last edited by Wraithan; Jun 5th, 2009 at 11:50 pm.
•
•
Join Date: Jun 2008
Posts: 128
Reputation:
Solved Threads: 31
We do not make private attributes in python, except in very rare cases. You can shadow the attribute access with "property" decorator later.
The Item and the Project class is trying to reimplement the named tuple, which is present in python 2.6 or later.
From a design point of view I find it unfortunate, that you provide interface in the TodoList to add an item to a project.
Another problem is that you do not have any Item without Project. So Items aggregate (in uml sense) to a Project.
It is unforunate too, that your TodoList.addItem calls the items project to add the item to its own project
The Item and the Project class is trying to reimplement the named tuple, which is present in python 2.6 or later.
From a design point of view I find it unfortunate, that you provide interface in the TodoList to add an item to a project.
Another problem is that you do not have any Item without Project. So Items aggregate (in uml sense) to a Project.
It is unforunate too, that your TodoList.addItem calls the items project to add the item to its own project
![]() |
Similar Threads
- Starting Python (Python)
- Python and Flash AS3 (Python)
- Looping through Nested Object Arrays Hangs Browser (JavaScript / DHTML / AJAX)
- newbee: python link objects (Python)
- simple random function (Python)
- How do I work with data files? (C#)
- Help in counting (Python)
Other Threads in the Python Forum
- Previous Thread: Py2exe problem
- Next Thread: help in this code
Views: 212 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied alarm aliased ansi backend basic beginner casino changecolor code corners curves customdialog dan08 definedlines dictionary digital dynamic editing events examples excel exe file filename float font format ftp function getvalue graphics gui handling homework iframe import input java line linux list lists loop matching mouse mysql newb number numbers numeric output parameters parsing path port prime program programming progressbar projects py py2exe pygame pyqt python random recursion recursive schedule screensaverloopinactive scrolledtext searchingfile skinning ssh stamp statistics stdout string strings table tails terminal text thread threading time tkinter tlapse tuple tutorial type ubuntu unicode url urllib urllib2 variable windows wxpython





