I have the following architecture:
+-----------------+
| Collection |
| |
| |
| |
| |
+-----------------+
| ^
| |
Create() | |
| |
v | GetItem(B)
+------------+ |
| Item A | |
| | |
| +------+
| |
+------------+
A class managing a collection of items, creates items. And these items may need other items from the collection.
The actual code is in python, and for now the Collection
passes itself as a parameter to the created Item
. As I see it, it is a bad practice. The only improvement I can see it to pass the few functions of Collection
needed by the Item
, instead of the whole instance.
For example:
class Collection:
GetItem(self, id):
...
CreateItem(self, id):
item = Item(id, self) # <-- pass self
...
class Item:
__init__(self, id, col):
...
col.GetItem(...)
How can I avoid passing self as parameter? Or is it a common usage in Python?
Aucun commentaire:
Enregistrer un commentaire