Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm sure you've heard all this before, but I'll give it a go. </p> <p>Classes are a way to group up a bunch of function and variables into a single object. When you get all the way down to it, this is simply a way of organizing everything into groups that make sense. There are benefits down the road for making things easier to understand, debug, extend, or maintain, but basically its just a way to make something more defined in your mental model.</p> <p>Your code looks like you are trying to write your entire program inside an 'object' (really, you just have an incorrectly written function).</p> <p>Consider this instead. </p> <p>Think of your mental model of rooms which have doors to them and whiteboards in them. Doors have a color. Also, whiteboards can have some text written on them. We'll leave it there to be simple.</p> <p>To me, this suggests 3 different objects -- a door object that has a string for color, a whiteboard object that has a string for the text, and a room object that has a door and a whiteboard.</p> <p>Consider the following code:</p> <pre><code>class Door(object): def __init__(self, color): self.color = color class Whiteboard(object): def __init__(self, default_text=''): self.text = '' self.write_text(default_text) def write_text(self, text): self.text += text def erase(self): self.text = '' class Room(object): def __init__(self, doorcolor, whiteboardtext=''): self.whiteboard = Whiteboard(whiteboardtext) self.door = Door(doorcolor) # make a room with a red door and no text on the whiteboard room1 = Room('red') # make a room with a blue door and 'yeah, whiteboard' on the whiteboard room2 = Room('blue', 'yeah, whiteboard') # make a room with a green door room3 = Room('green') # now I can play around with my 'rooms' and they keep track of everything internally print 'room 1 door color: ' + room1.door.color print 'room 2 door color: ' + room2.door.color # all my rooms have a door and a whiteboard, but each one is different and self contained. For example # if I write on room 1's whiteboard, it doesn't change anything about room 3s print 'room1 whiteboard: ' + room1.whiteboard.text print 'room2 whiteboard: ' + room2.whiteboard.text print 'room3 whiteboard: ' + room3.whiteboard.text print '-- changeing room 1 whiteboard text --' room1.whiteboard.write_text('oop is really helpful') print 'room1 whiteboard: ' + room1.whiteboard.text print 'room2 whiteboard: ' + room2.whiteboard.text print 'room3 whiteboard: ' + room3.whiteboard.text </code></pre> <p>The <strong>init</strong> function is what gets called when you 'initialize' a new instance of your class. In the example I am making 3 Room objects which each create a Door and Whiteboard object internally. The parameters I pass into the constructor <code>Room(parameter1, parameter2)</code> get passed to the <strong>init</strong> functions - you can see I'm using this to set the door color and optionally some text on the whiteboard. Also notice that the variables that 'belong' to the objects are referenced with <code>self</code> - this reference is what gets passed in as the first parameter to all class functions (and becomes more important later when you are extending classes and other more advanced things).</p>
    singulars
    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.
    3. VO
      singulars
      1. This table or related slice is empty.
    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