Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's kinda hard to understand what you're trying to achieve from your post, but I'll try to give you examples on what I think you're trying to do.</p> <p>Let's begin by focusing on only <code>Box</code>-class, and ignore the <code>Item</code> for now. Here's what you have:</p> <pre><code>class Box(): id = 0 def __init__(self,id): Box.id = id def __get__(self): return Box.id </code></pre> <p>The <code>__get__</code> method is useless, simply don't use it here, you don't need it. Now what we notice, is you set <code>Box.id = 0</code>, this means that you can call <code>print(Box.id)</code> and it will print <code>0</code>. You can also init a new box instance, and you can call <code>print(box_instance.id)</code>, which will also print <code>0</code> as expected.</p> <p>However, in your <code>__init__</code> method, you change <code>Box.id</code>, instead of the instance's own id, is this what you meant to do? Here's a quick example on what will happen with your code:</p> <pre><code>In: Box.id Out: 0 a = Box(1) In: a.id Out: 1 In: Box.id Out: 1 b = Box(2) In: b.id Out: 2 In: a.id Out: 2 In: Box.id Out: 2 </code></pre> <p>As you can notice here (or test yourself), when initializing new <code>Box</code> object, it change's <code>Box</code> class' id, and not the id of the instance. This way every instance's id changes if you init a new <code>Box</code> -instance. This is caused cause it's not actually the id of the instance, but it's the id of <code>Box</code>.</p> <p>If this is the behavior you want, feel free to use it (you still don't need <code>__get__</code>), but if you want each Box to have their unique id, use <code>self</code> instead of box.</p> <pre><code>class Box: id = 0 # This is Box class' id, it's not even necessary to define it def __init__(self, id): # This will be each instance's unique id self.id = id #self.id means the instance's unique id </code></pre> <hr> <p>Now that we got the <code>Box</code> pretty much done, we can start working on the <code>Item</code>. I'm quite unsure of what you're trying to achieve with this, but taking a look at your code, it seems like you're actually trying to give each item an unique id (which is a good idea), and then you want to give them <code>box_id</code> which is simply <code>Box.id</code>, the class' id itself? This doesn't make any sense to me.</p> <p>What I ACTUALLY think you're trying to achieve: Insert items into boxes, and this way you want item to know it's "parent" box's id. It could be done as following:</p> <pre><code>class Item: #Notice how inheriting is not needed at all!! def __init__(self, id, parent): self.id = id self.parent = parent # I prefer calling it parent, call it what ever you want, box works too </code></pre> <p>Now what I'd also suggest, is:</p> <ol> <li>Inside <code>Box.__init__()</code>, define a list <code>self.items = []</code></li> <li>Inside <code>Item.__init__()</code>, append <code>self</code> to parent's items. <code>self.parent.items.append(self)</code></li> </ol> <p>This way your items know the box they're in, and boxes know which items are in them.</p> <hr> <p><strong>Edit:</strong> A short example code on how to use the parent:</p> <pre><code>box1 = Box(1) #box1 has id of 1 box2 = Box(2) #box2 has id of 2 item1 = Item(3, box1) #item1 has id of 3, parent box1 item2 = Item(4, box2) #item2 has id of 4, parent box2 item3 = Item(5, box1) #item3 has id of 5, parent box1 In: item1.id Out: 3 In: item1.parent.id Out: 1 In: item2.id Out: 4 In: item2.parent.id Out: 2 In: item3.id Out: 5 In: item3.parent.id Out: 1 In: item3.parent == item1.parent Out: True </code></pre> <p>As we see here, item can directly call it's parent's methods and use it's attributes. This way you can have multiple items in a box, (<code>item1</code> and <code>item3</code> both have same parent) and each of those items' <code>parent</code> attribute points to the box.</p> <p>Basically <code>item1.parent == box1</code> so <code>item1.parent.id</code> is what you called <code>item.box_id</code> in your code.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload